循环查询使用META_VALUE排除META_KEY

时间:2015-05-22 作者:Ingo Meinhard

嗨,我使用这样的循环:

// show all active coupons for this store and setup pagination
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
query_posts( array(
    \'post_type\' => APP_POST_TYPE,
    \'post_status\' => \'publish\',
    APP_TAX_STORE => $term->slug,
    \'ignore_sticky_posts\' => 1,
    \'posts_per_page\' => -1,
    \'paged\' => $paged
) );
现在我有了一个元值meta_key\' => \'clpr_excoupon\' 它是1或0。

我想排除所有meta_key\' => \'clpr_excoupon\' 具有\'meta_value\'=> 0, 还有“不存在”需要帮助!!

3 个回复
SO网友:Nicolai Grossherr

NOT 使用query_posts() - 是的bad, 确切地说,非常糟糕。

有关更多信息,请阅读:

使用WP_Query 相反

示例meta_key, meta_valuemeta_compare 正在使用的参数:

$args = array(
    \'meta_key\'     => \'clpr_excoupon\',
    \'meta_value\'   => \'0\',
    \'meta_compare\' => \'!=\'
);
$query = new WP_Query( $args );
注意:这些参数适用于query_posts 也是,但正如所说,你不应该使用它。

更新:

具有NOT EXISTS 比较

$args = array(
    \'meta_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'key\'     => \'clpr_excoupon\',
            \'compare\' => \'NOT EXISTS\',
            \'value\'   => \'prior to WP 3.9 a value was needed due to bug #23268\'
        ),
        array(
            \'key\'     => \'clpr_excoupon\',
            \'compare\' => \'!=\',
            \'value\'   => \'0\'
        ),
    ),
);
$query = new WP_Query( $args );

SO网友:Mile Milosheski

您应该对此使用2个查询:

$excluded = query_posts( array(
        \'meta_key=> \'clpr_excoupon\';
        \'meta_value\' => \'0\'
) );

//$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
query_posts( array(
    \'post__not_in => $excluded;
    \'post_type\' => APP_POST_TYPE,
    \'post_status\' => \'publish\',
    APP_TAX_STORE => $term->slug,
    \'ignore_sticky_posts\' => 1,
    \'posts_per_page\' => -1
    //\'paged\' => $paged
) );
使用此代码,您将获得所有具有clpr\\u excoupon且值为1的帖子:)

希望这有帮助。。。

干杯,迈尔

SO网友:Ingo Meinhard

它正在处理此问题,感谢@ialocin的帮助:

// show all active coupons for this store and setup pagination
                    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
                    query_posts( array(
                        \'post_type\' => APP_POST_TYPE,
                        \'post_status\' => \'publish\',
                        APP_TAX_STORE => $term->slug,
                        \'ignore_sticky_posts\' => 1,
                        \'posts_per_page\' => -1,
                        \'meta_query\' => array(
    \'relation\' => \'OR\',
    array(
        \'key\'     => \'clpr_excoupon\',
        \'compare\' => \'NOT EXISTS\'

    ),
    array(
        \'key\'     => \'clpr_excoupon\',
        \'compare\' => \'!=\',
        \'value\'   => \'1\'
    ),
),
                        \'paged\' => $paged
                    ) );

结束

相关推荐

Loop for sticky posts

我用过Justin Tadlock\'s 关于如何创建仅包含粘性帖子的循环的教程。代码大致如下所示:$sticky = get_option( \'sticky_posts\' ); rsort( $sticky ); $sticky = array_slice( $sticky, 0, 2 ); query_posts( array( \'post__in\' => $sticky, \'caller_get_posts\' => 1 ) ); 根据教程,我