skip to Main Content

I am trying to remove the first three posts of a taxonomy from the main query. I’ve run a whole bunch of ideas and examples from around the internet but none seem to be achieving what I need or just throw up errors. I believe it needs to utilize pre get posts, which I’ve got the code below for as far as I’ve got. It’s not great but this has left me scratching my head and I can’t seem to find any solutions out there.

function excluded( $query ) {
    if( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
        $query->set( 'tag__not_in', '8384' );
    }
}
add_action( 'pre_get_posts', 'excluded' );

2

Answers


  1. Chosen as BEST ANSWER

    This was the code that ended up working perfectly for me. If there is a less expensive method or more efficient way, would certainly be open to it

    function excluded ( $query ) {
    if( !is_admin() && $query->is_main_query() && $query->is_home() ) {
    $removal = new WP_Query( array('posts_per_page' => 3,'tax_query' => array(array('taxonomy' => 'post_tag','field' => 'id','terms' => '1234' ))) );
    if($removal){$those_ids = wp_list_pluck($removal->posts, 'ID');$query->set('post__not_in', $those_ids);}}}
    add_action( 'pre_get_posts', 'excluded' );
    

  2. You can try to use the below code, By this, you can exclude the posts by their ids.

    'post__not_in' => array('post_id_1','post_id_2','post_id_3');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search