Exclude Featured Post from Custom Query WordPress

This is the custom query I’m running to display post from 2 categories.
I have installed WordPress Plugin “Featured Post” but the featured post is not excluding from the displayed list.

<?php 
     $category_id = get_cat_ID($strReports || $strInsights);
     $custom_query = new WP_Query( 'cat=' .$category_id. '&featured=no&posts_per_page=6&order=desc' );
     while($custom_query->have_posts()) : $custom_query->the_post();
?> 

HTML Content Here

<?php endwhile; ?>
<?php wp_reset_query(); // reset the query ?>

Related posts

3 comments

  1. Looking through “Featured Post” you can see that it just test against yes value. What feature=yes does is just check a meta field, so I think you can do the reverse way to achieve what you want, like this:

    $args = array(
        'cat' => $category_id,
        'posts_per_page' => 6,
        'order' => 'DESC', // since order default value is already DESC you can remove this line
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => '_is_featured',
                'value' => 'yes',
                'compare' => '!=',
            ),
            array(
                'key' => '_is_featured',
                'value' => 'foo', // Prior to WP 3.9 we have to provide any non-empty string here
                'compare' => 'NOT EXISTS',
            ),
        ) ,
    );
    $custom_query = new WP_Query($args);
    

    Hope it helps!

  2. your’e running an invalid argument on get_cat_ID.

    should be something like this:

    <?php 
         $cat_names  = array('Cat_Name1', 'Cat_Name2');
         $category_Name = implode(',', $cat_names);
         $args = array(
            'category_name' =>  $category_Name,
            'posts_per_page' => 6,
            'meta_query' => array(
                    array(
                        'key' => 'featured', 
                        'value' => true, 
                        'compare' => '!=',
                    ),
                ), 
         );
         $custom_query = new WP_Query( $args );
         while($custom_query->have_posts()) : $custom_query->the_post();
    ?> 
    
    <?php the_title();?>
    
    <?php endwhile; ?>
    <?php wp_reset_query(); // reset the query ?>
    
  3. create your custom query and add filter.

    function SearchFilter($query){
    
    $query=" SELECT Distinct SQL_CALC_FOUND_ROWS p.* FROM `$wpdb->posts` as p left join `$wpdb->postmeta` as m on m.post_id=p.ID "
                    ." left join `$wpdb->term_relationships` as r ON (p.ID = r.object_id) "
                    ." left join `$wpdb->term_taxonomy` as x ON (x.term_taxonomy_id = r.term_taxonomy_id) "
                    ." left join `$wpdb->terms` as t ON (t.term_id = x.term_id AND x.taxonomy='category') "
                    ." WHERE p.post_type = 'post' AND p.post_status = 'publish' "
                    ." AND t.term_id='$idcategorie' "
    
    return $query;
    }
    add_filter( 'posts_request', 'SearchFilter' );
    

Comments are closed.