简体   繁体   中英

Exclude featured posts through custom query not working

I am trying to exclude posts from query and it is not working at all.

Here what I tried

 <?php
                                    $args = array(
                                        'post_type' => 'videos-presentations',
                                        'post_status' => 'publish',
                                        'posts_per_page' => 4,
                                        'paged' => $paged,
                                        'meta_query' => array(
                                            array(
                                                'meta_key' => '_is_ns_featured_post',
                                                'meta_value' => 'yes',
                                                'meta_compare' => '!='
                                            )
                                        )
                                    );
                                    $my_query = new WP_Query($args);
                                    ?>

Also Tried with

'meta_compare' => 'NOT EXIST'

and

 'meta_compare' => 'NOT IN'

Any idea what I am doing wrong?

Got it. From here

It Works with just

'meta_query' => array(
                         array(
                                 'key' => '_is_ns_featured_post',
                                 'compare' => 'NOT EXISTS'
                              ) 
                     )
function exclude_posts ( $query ) {
    $meta_query = $query->get( 'meta_query' );

    $meta_query[] = array(
                        'key'=>'_is_ns_featured_post',
                        'value'=>'yes',
                        'compare'=>'!=',
                    );
    $query->set( 'meta_query',$meta_query );
}

add_action( 'pre_get_posts', 'exclude_posts' );

place this code in functions.php file of active theme

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