1

After reading through here and trying various options, I am finally able to get my custom post types 'testimonial' (created with product 'Toolset') to show along with my standard cat/tag queries WITHOUT the menu disappearing!! Here is the code I came up with, and placed in my theme's custom-functions.php file:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category() || is_tag() || is_home() && empty($query->query_vars['suppress_filters'] ) ) {
$post_type = get_query_var('post_type');
if($post_type)
    $post_type = $post_type;
else
    $post_type = array('post','testimonial','nav_menu_item');
$query->set('post_type',$post_type);
return $query;
 }
}

Being new to this I am not quite certain of how this is relating to the loop, and so of course now the problem is, when pulling my custom post types, on that same page a footer widget pulling from the same custom post type is delivering odd results, so it seems clear that I need to apply wp_reset_query(); or wp_reset_postdata(); - but my efforts to apply it are not working. I have tried it within the last container of the query itself, and in various theme locations. It has me wondering if I am really not doing it right, or there is instead a fatal flaw lurking in my code??? I am hoping someone can save me on this it has been so frustrating...


ADDENDUM - Updating this based on comments received...

If it helps others, I tried a few changes from suggestions below, but didn't follow them correctly at first... so for example was mixing '$query->is_category() || $query->is_tag()', etc with just 'is_main_query()' (duh) and seeing it fail assumed the latter addition was not helping... but then I read again and followed the complete suggestion applying the following:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if($query->is_category() || $query->is_tag() || $query->is_home() && $query->is_main_query()) {
$post_type = get_query_var('post_type');
if($post_type)
    $post_type = $post_type; 
else
    $post_type = array('post','testimonial');
$query->set('post_type',$post_type);
return $query;
    }
}

... and it now appears to be working correctly! So just as stated below, not including $query-> into every one of the statement elements brought on the bad mojo!

It also seems that I am now able to eliminate the && empty($query->query_vars['suppress_filter'] as well as the addition of 'nav_menu_item' since the filter is now being applied to the proper location. Thanks much for the help with this.

2 Answers 2

1

pre_get_posts runs on every query, so you want to check if is_main_query() so it only applies to the default query and not additional queries you run on the page.

You should also get in the habit of referencing the query object passed to the function like $query->is_category(), as calling is_category() is a wrapper for $wp_query->is_category(), which may also have unexpected results depending on what you're trying to do.

2
  • Hi, thanks for this... I tried updating my code to incorporate both of your thoughts, but not able to see positive effect - but am starting to understand better how this routine is effecting other elements... I will be updating my original post shortly, I would appreciate any addition thoughts you might have once I do... thanks very much
    – Soyo
    Commented Feb 16, 2013 at 10:57
  • Check that - your answer helped me more than I thought... and while I need to now check and verify, I think I have it working now! I will post my findings above thanks
    – Soyo
    Commented Feb 16, 2013 at 11:14
0

It looks like you are just trying to get WordPress to include your custom post type in the queries. All you should need to do for that is to register the post type with 'public' => true. Like this example from the Codex:

function codex_custom_init() {
    $args = array( 'public' => true, 'label' => 'Books' );
    register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );

Have I misunderstood you?

5
  • Yes, thanks for your thoughts on this, you are exactly right... however, the above simply doesn't do it for me (I tried your example, replacing 'Books' with 'testimonials' and 'book' with 'testimonial'). I am also starting to understand better why I am getting strange results from my code, which I will update on the post above shortly...
    – Soyo
    Commented Feb 16, 2013 at 10:55
  • I am glad you got this working (since you accepted @Milo's answer) but that seems a long way around for what should be more or less built in.
    – s_ha_dum
    Commented Feb 16, 2013 at 14:36
  • @s_ha_dum WordPress doesn't include custom post types in category, tag, or home main queries, they have to be added if you want them to show up there, otherwise it shows just the post post type.
    – Milo
    Commented Feb 16, 2013 at 15:32
  • Yeah your answer is def. the way it should have worked vs. having to mess with queries and filters - though I'm starting to find w/WP there are more than a few hoops to jump through. Really appreciate the responses!
    – Soyo
    Commented Feb 16, 2013 at 17:12
  • See Milo's comment above.
    – s_ha_dum
    Commented Feb 16, 2013 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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