可视化作曲家自定义查询 - 不包括meta_key

时间:2017-01-23 02:03:16

标签: php wordpress posts visual-composer

我有一个自定义查询,我想帮助转换为可视化作曲家的自定义查询。基本上,我想排除所有帖子在帖子网格中显示的meta_key:_is_featured_posts,其值为yes。

// WP_Query arguments
$args = array(
    'post_type'              => array( 'post' ),
    'post_status'            => array( 'publish' ),
    'nopaging'               => false,
    'posts_per_page'         => '12',
    'order'                  => 'DESC',
    'orderby'                => 'date',
    'meta_query'             => array(
        'relation' => 'AND',
        array(
            'key'     => '_is_ns_featured_post',
            'value'   => 'yes',
            'compare' => 'NOT EXISTS',
        ),
    ),
);

// The Query
$query = new WP_Query( $args );

任何帮助都将不胜感激。

由于

2 个答案:

答案 0 :(得分:0)

有一种替代解决方案,不推荐使用,但由于NOT EXISTS不起作用,因此您可以使用以下代码。我也检查了here给出的解决方案,但它也没有工作。

//to hold the post id which has _is_ns_featured_post => 'yes'
$exclude_id = array();

$args_exclude = array(
    'post_type' => array('post'),
    'post_status' => array('publish'),
    'posts_per_page' => '-1',
    'meta_query' => array(
        array(
            'key' => '_is_ns_featured_post',
            'value' => 'yes',
        ),
    ),
);

$exclude_posts = new WP_Query($args_exclude);
if (!empty($exclude_posts->posts))
{
    foreach ($exclude_posts->posts as $post)
    {
        $exclude_id[] = $post->ID;
    }
}


$args = array(
    'post_type' => array('post'),
    'post_status' => array('publish'),
    'nopaging' => false,
    'posts_per_page' => '12',
    'order' => 'DESC',
    'orderby' => 'date',
    'post__not_in' => $exclude_id //exclude post_id which has _is_ns_featured_post => 'yes'
);

// The Query
$query = new WP_Query($args);
foreach ($query->posts as $post)
{
    print_r($post);
}

希望这有帮助!

答案 1 :(得分:0)

请参阅:visual composer wordpress query for post grid

试试这个:

$args = array(
    'post_type'              => array( 'post' ),
    'post_status'            => array( 'publish' ),
    'nopaging'               => false,
    'posts_per_page'         => '12',
    'order'                  => 'DESC',
    'orderby'                => 'date',
    'meta_query'             => array(
        'relation' => 'AND',
        array(
            'key'     => '_is_ns_featured_post',
            'value'   => 'yes',
            'compare' => 'NOT EXISTS',
        ),
    ),
);

echo http_build_query($args);

//结果:

post_type%5B0%5D=post&post_status%5B0%5D=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=_is_ns_featured_post&meta_query%5B0%5D%5Bvalue%5D=yes&meta_query%5B0%5D%5Bcompare%5D=NOT+EXISTS

http://sandbox.onlinephpfunctions.com/code/5c2bc6ddd37a02fc8facf4f227176e262854b92e

如果只有一种帖子类型,我建议避免使用数组('post'),所以只需使用post_type=post&post_status=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query[relation]=and&meta_query[0][key]=_is_ns_featured_post&meta_query[0][value]=yes&meta_query[0][compare]=NOT EXISTS

P.S。 可能%5B%5D您需要通过[转换回]echo urldecode(http_build_query($args));