• Resolved willrich33

    (@willrich33)


    Hi:

    I am trying to get a category main query to include another category. It seems that cat_ID is set in the $query object and there seems to be no way to alter it. What I really want to do is alter or set the main query to all posts when it is set to a category.

    Any ideas? This is my last try!

    add_action( 'pre_get_posts', 'hfo_modify_alter_insights_to_all' );
    // Modify the current query
    function hfo_modify_alter_insights_to_all( $query ) {
    	// Check if on frontend and main query is modified
    	if (!$query->is_main_query() || is_admin()) return;
    	if ($query->is_category && is_category(1)) {
    		   $taxquery = array(
    				array(
    					'taxonomy' => 'category',
    					'field' => 'term_id',
    					'terms' =>  array(7) ,
    				)
    			);
    
    		$query->set('tax_query', $taxquery);
    	}
        return $query;
    }

    Thanks, Willy

Viewing 1 replies (of 1 total)
  • Thread Starter willrich33

    (@willrich33)

    Hi:

    Got it. I was so close. 13 is the category I wanted to include.

    add_action( 'pre_get_posts', 'hfo_modify_alter_insights_to_all' );
    // Modify the current query
    function hfo_modify_alter_insights_to_all( $query ) {
    	// Check if on frontend and main query is modified
    	if (!$query->is_main_query() || is_admin()) return;
    	if ($query->is_category && is_category(1)) {
    		   $taxquery = array(
    				'relation'=>'OR',
    				array(
    					'taxonomy' => 'category',
    					'field' => 'term_id',
    					'terms' =>  array(13) ,
    				)
    			);
    
    		$query->set('tax_query', $taxquery);
    	}
        return $query;
    }

    Thanks, HTH, Willy

Viewing 1 replies (of 1 total)
  • The topic ‘pre_get_posts’ is closed to new replies.