• Resolved design_studio

    (@design_studio)


    Hello everyone. I am looking for some help excluding a post from my home page main query.
    I am using this code:

    add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
    
    function add_my_post_types_to_query( $query ) {
    	if ( is_home() && $query->is_main_query() )
    		$query->set( 'post_type', array( 'post', 'custom' ) );
    	return $query;
    }

    taken from WordPress Codex to display my custom post type on home page together with my regular posts. It is working perfectly and I don’t want to change anything. But let’s say I have a custom post type post with id=5 for example and I would like to exclude it from the above query. How to achieve that? Any help appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try using:

    function add_my_post_types_to_query($query)
    {
    	if ($query->is_home() AND $query->is_main_query())
    	{
    		$query->set('post_type', array('post', 'custom'));
    
    		$query->set('post__not_in', array(5));
    	}
    }
    
    add_action('pre_get_posts', 'add_my_post_types_to_query');
    Thread Starter design_studio

    (@design_studio)

    Worked exactly as I needed it. Thank you very, very much!!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to exclude a post from my home page main query?’ is closed to new replies.