简体   繁体   中英

Visual composer custom query - excluding meta_key

I have a custom query that I would like some help converting to visual composer's custom query. Basically, I would like to exclude all posts from displaying in the post grid that have the meta_key: _is_featured_posts and its value as 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 );

Any help would be appreciated.

Thanks

There is an alternative solution, it is not recommended but as NOT EXISTS is not working so you can use the following code. I also check the solution given here , but it's not working either.

//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);
}

Hope this helps!

See: visual composer wordpress query for post grid

Try this:

$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);

// Result:

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

I would recommend to avoid use array('post') in case if only one post type, so just use 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

PS possibly %5B and %5D you will need to convert back to [ and ] via echo urldecode(http_build_query($args));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM