1

I would like to display all the posts I have and custom post type pages if they have a certain tag on my home page. This is my code, but I can't get it to work:

add_filter( 'pre_get_posts', 'get_posts_plus_cpt_with_certain_tag' );

function get_posts_plus_cpt_with_certain_tag( $query ) {
   if ( is_home() && $query->is_main_query() ) {
      if ( $query->query_vars['post_type'] == 'cpt' ) {     
         $taxquery = array(
            array(            
               'taxonomy' => 'cpt-tag',
               'field' => 'id',
               'terms' => 1
               )
            );      
            $query->set('tax_query', $taxquery);        
         }  
      $query->set( 'post_type', array( 'post', 'cpt' ) );               
      return $query;
   }
}

I'm stuck, any help would be appreciated. Thanks!

Edit: updated the code, thanks to Pieter Goosen

add_action( 'pre_get_posts', 'get_posts_plus_cpt_with_certain_tag' );

function get_posts_plus_cpt_with_certain_tag( $query ) {
   if ( $query->is_home() && $query->is_main_query() ) {
      if ( 1 == 1 ) {   
         $taxquery = array(
            array(            
               'taxonomy' => 'cpt_tag',
               'field' => 'term_id',
               'terms' => 27
               )
            );      
            $query->set('tax_query', $taxquery);        
         }  
      $query->set( 'post_type', array( 'post', 'cpt' ) );
   }
}
  • My problem is not fully solved yet, but I accepted your answer anyway, because it was truly helpful, thanks again. :) I commented below your answer, I'm just not very quick at typing, sorry for that! – pot63 May 6 '15 at 8:00
  • My edit should solve your issue completely. You just needed to remove post from the post type array :-) – Pieter Goosen May 6 '15 at 8:02
1

You have a couple of issues here

  • is_home() should be a method of $query, so it should become $query->is_home()

  • The value to the field parameter should be term_id, not id. id works, so will any crap passed as value because if you look at the source codes, the source only checks for slug and name, it does not specifically check for term_id. So any value will work as default. This is wrong. The correct and only value for term ids is term_id

  • pre_get_posts is an action, not a filter

  • Actions do not return, so remove return $query

  • 1 cannot be your term id. 1 is the (reserved) term id of the default category term uncategorized. This is why your query fails to get posts. uncategorized is a term to the build in taxonomy category, not the custom taxonomy cpt-tag

  • Your custom taxonomy cannot be cpt-tag. Taxonomy names should always be lowercase letters (caveat - seems numbers are allowed as well if not the first digit) with multiple words separated by an underscore only. Hyphens should be avoided as it creates issues with template names etc

  • Your conditional check if ( $query->query_vars['post_type'] == 'cpt' ) {} is also probably never returning true due to being the home page. The default post_type on the homepage is post, always if not modified by some kind of filter like pre_get_posts. I would just remove this.

EDIT

From your edit, and comment, you need to remove post from the post_type array if you don't need the deafult post type posts to display. You also do not need the 1 == 1 check.

Your final code should be:

add_action( 'pre_get_posts', 'get_posts_plus_cpt_with_certain_tag' );

function get_posts_plus_cpt_with_certain_tag( $query ) {
   if ( $query->is_home() && $query->is_main_query() ) {

         $taxquery = array(
            array(            
               'taxonomy' => 'cpt_tag',
               'field' => 'term_id',
               'terms' => 27
               )
            );      
            $query->set('tax_query', $taxquery);        
         }  
      $query->set( 'post_type', 'cpt' );

}
  • Wow, so many mistakes, thanks for pointing them out! I really appreciate your detailed explanation, I updated the code and changed the second if statement to 1 == 1. So the $query->set('tax_query', $taxquery); is working now, but now of course it applies not only to cpt post type, but posts too. Is there a way I can apply it only to cpt post type? – pot63 May 6 '15 at 7:52
  • Please see my update. It should work as expected now :-) – Pieter Goosen May 6 '15 at 8:00
  • Oh, it's working, but I would like to display all the posts too (unfiltered), and the cpt post type pages (filtered by the tax_query). Sorry for not being clear enough! :) – pot63 May 6 '15 at 8:10
  • I have answered this question in the context it was written. What you need to do now is much more complex than what is given in my answer. As this issue of this particular question is addressed, you should start a new question with this new requirements. And be very sure to include all info as questions are answered within the context in the question :-) – Pieter Goosen May 6 '15 at 8:19
  • Ok, thanks! :) I started a new question: wordpress.stackexchange.com/questions/187406/… (I think the if's closing curly brace should be after $query->set( 'post_type', 'cpt' );, not before it. :)) – pot63 May 6 '15 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.