Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In the theme I'm using there is a custom.php file where the following code issues a pre_get_post function which adds the custom post types to the original query object.

I've learned that you cannot conditionally load this... and it is breaking BBPress pages on forums.php by refusing BBP to fully load certain post types (topics, forums etc).

  if (!is_admin()){

    add_filter('pre_get_posts', 'query_post_type');

    function query_post_type($query) {

          global $oswcPostTypes;

          if(empty( $query->query_vars['suppress_filters'] ) ) {
            $post_type = get_query_var('post_type');

            global $oswc_reviews;   
            // BEGIN CULPRIT CODE 
            if($post_type ) {
                $post_type = $post_type;
                $query->set('post_type',$post_type);
                return $query;  
            // END CULPRIT CODE

            } elseif(!is_page() && !is_preview() && !is_attachment() && !is_search() ) {    
                $post_type = array('post');
                foreach($oswcPostTypes->postTypes as $postType){
                    array_push($post_type, $postType->id);
                }
                $query->set('post_type',$post_type);
                return $query;
            }   
          }
        }
    }

Thanks in advance, this is a tough one.

EDIT: Fixed thanks to Brian McCulloh. He sent me his new theme which had a revised PGP function. The old one WAS keeping certain post types from the query, such as forum and topic PT's... Corrected Code:

if(!is_admin()) {   
    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      //get review types
      global $oswcPostTypes;    
      if(empty($query->query_vars['suppress_filters'])) {
        $post_type = get_query_var('post_type');    

        if(!$post_type && !is_page() && !is_preview() && !is_attachment() && !is_search()) {    
            $post_type = array('post');
            foreach($oswcPostTypes->postTypes as $postType){
                array_push($post_type, $postType->id);
            }
            $query->set('post_type',$post_type);
            //the returned array contains the "post" post type and all of the user-defined review types
            return $query;              
        }
      }
    }
}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

I'm not 100% clear on what the problem is, but I would start with putting the conditions inside the function instead of wrapping the entire function.

add_filter('pre_get_posts', 'query_post_type');

function query_post_type($query) {

  if (!is_admin()){

      global $oswcPostTypes;

      if(empty( get_query_var('suppress_filters' ) ) {
        $post_type = get_query_var('post_type');
        //get theme options
        global $oswc_reviews;   
        if($post_type ) {
            $post_type = $post_type;
            set_query_var('post_type',$post_type);
            return $query;  

        } elseif(!is_page() && !is_preview() && !is_attachment() && !is_search() ) {    
            $post_type = array('post');
            foreach($oswcPostTypes->postTypes as $postType){
                array_push($post_type, $postType->id);
            }
           set_query_var('post_type',$post_type);
            return $query;
        }   
      }
    }
}
share|improve this answer
    
you're my internet hero, you've came to my rescue in the past but I spent 2 days trying to work with conditionals like you recommended before posting it here. The culprit code was making the query carry strictly certain post types... –  BenRacicot Mar 3 '13 at 0:26
    
Lol... thanks for the compliment! I'm glad that solved it for you. Don't forget to accept it as the answer. –  helgatheviking Mar 3 '13 at 0:56
    
I don't think conditionals is the answer, correct? pre_get_posts fires before conditionals can work if I understand correctly. You actually have to change the query. Let me know and I will do what you say to. –  BenRacicot Mar 3 '13 at 18:01
    
I don't think so. I think the query is set, but the posts haven't been returned. So you can use conditional logic. I didn't really understand the problem you were describing, so couldn't offer much help with that specifically, but I do prefer to put my conditionals inside the functions. –  helgatheviking Mar 3 '13 at 19:34
    
Ah, Ok. My understanding is that the query object has been created but not sent to retrieve anything, yet. –  BenRacicot Mar 3 '13 at 23:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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