Query reset problem or flawed code to enable custom post type query

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…

Read More

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.

Related posts

Leave a Reply

2 comments

  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. 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?