• Hi there,

    I am trying to exclude a custom post_type category from displaying on the homepage. I am half way there…

    If I remove the <if> statements in the following code it works perfectly but I need them otherwise the function is applied to all pages in the site, not just the home page.

    I have a static front page configured.

    This is the code I have been trying to use:

    add_action('pre_get_posts','exclude_cat');
    function exclude_cat( $query ) {
    	if ( !is_admin() && $query->is_main_query() ) {
    		$tax_query = $query->get( 'tax_query' ) ?: array();
    		$tax_query[] = array(
    			'taxonomy' => 'st_category',
    			'field'    => 'id',
    			'terms'    => array( 115 ),
    			'operator' => 'NOT IN',
    		);
    		$query->set( 'tax_query', $tax_query );
    	}
    	return $query;
    }

    I have also tried the following <if> statements with no success:

    if( $query->is_page( array( 113, 'portfolio', 'Portfolio' ) ) ) {

    if( $query->is_home() ) {

    if( $query->is_front_page() ) {

    What is going on?

    Please help if you can – thank you so much!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello Bytelab,

    try ($query->query_vars[‘page_id’] == 113) as the first condition. is_front_page() does not work, because pre_get_posts runs before WP_Query has been setup (https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts).

    Thread Starter Bytelab

    (@bytelab)

    Hi buntegiraffe – thanks so much.

    I have tried the following based on your suggestion but again it just renders the function seemingly obsolete and shows all posts again rather than excluding those with the post_type category id of 115.

    add_action('pre_get_posts','exclude_cat');
    
    function exclude_cat( $query ) {
            if($query->query_vars['page_id'] == 113) {
    		$tax_query = $query->get( 'tax_query' ) ?: array();
    		$tax_query[] = array(
    			'taxonomy' => 'st_category',
    			'field'    => 'id',
    			'terms'    => array( 115 ),
    			'operator' => 'NOT IN',
    		);
    		$query->set( 'tax_query', $tax_query );
    	}
    	return $query;
    }

    I am really lost as to why this is happening… no doubt something simple though.
    If you have any further ideas I will be very grateful.

    Thanks!

    Thread Starter Bytelab

    (@bytelab)

    Success! I have found a solution using get_the_ID() …

    add_action('pre_get_posts','exclude_cat');
    function exclude_cat( $query ) {
            if ( get_the_ID() == '113' ) {
    		$tax_query = $query->get( 'tax_query' ) ?: array();
    		$tax_query[] = array(
    			'taxonomy' => 'st_category',
    			'field'    => 'id',
    			'terms'    => array( 115 ),
    			'operator' => 'NOT IN',
    		);
    		$query->set( 'tax_query', $tax_query );
    	}
    	return $query;
    }

    Thanks again 😀

    • This reply was modified 7 years, 7 months ago by Bytelab.
    • This reply was modified 7 years, 7 months ago by Bytelab.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Exclude custom post type category from homepage’ is closed to new replies.