I have built a plugin that uses custom post types. Everything works exactly as it should except when you list posts by category; the recent post widgets just displays the recent custom posts type.

http://testblog.co.uk/events-list/ - All OK (these are the custom posts)

http://testblog.co.uk/category/derek/ - Only Recent custom posts display

The plugin has a wp_query call:

$args = array('post_type'=>'event','orderby'=>'meta_value_num','meta_key'=>'event_date','order'=>'asc','posts_per_page'=>-1);
query_posts( $args );

But I can't see how this would affect things. Any suggestions as to where I should start looking?


Not using pre_get_post so that's not the issue. I do use:

$output_string = ob_get_contents();
return $output_string;

after clearing the query but when I removed this it made no difference.

It was a user that alerted me to the problem so it's not my theme (although I have tested using 2014 and all other plugins deactivated).

It's got to be a reset not firing in the right place...


Debug is now on and apart from a few undefined variables there isn't anything being reported.


The 'recent posts' list that appears seems totally random. It displays posts and events and deleted items - most bizarre,

Debugging is complete - there is one warning related to another plugin that can be ignored.

Taxonomy is

$args = array('taxonomies' => array('category','post_tag'));register_post_type('event',$args);

Obviously a lot more labels and args in the plug8in but that's the only reference to categories.

Tested using 'tags' and archive queries and al lis ok. So it's only to 'category' queries. Removing the category from the CPT taxonomy makes no difference.

  • "It was a user that alerted me to the problem so it's not my theme (although I have tested using 2014 and all other plugins deactivated)". I don't understand this line. Was your problem the same during these tests Sep 10, 2014 at 17:10
  • This was their email: Your plugin is causing conflicts with the recent posts widget when one goes to a category page. Refer to any regular page - then go to a category page like this: mainejuniors.org/category/alumni - the recent posts get messed up.
    – fisicx
    Sep 11, 2014 at 5:18
  • I tested using the 2014 theme and with all other plugins deactivated. The error persisted. It only happens when you are looking at a category page: mydomain.com/category/something. The only reference to categories in the plugin is in the CPT taxonomy.
    – fisicx
    Sep 11, 2014 at 5:20
  • Have you sorted out the debug errors. What is your taxonomy called Sep 11, 2014 at 5:23
  • See Edit 3 above
    – fisicx
    Sep 11, 2014 at 9:26

1 Answer 1


I don't really understand your question, but I see a couple of issues that might have a negative effect on other queries

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

  • Always use WP_Query or get_posts to create custom queries if you can't alter the main query with pre_get_posts to suite your needs.

  • The most probable cause for interference with other queries is the fact that you did not reset your custom query. This is very important as any custom query that has not been reset will have a negative effect on any other query on the same page. You can reset your query with wp_reset_postdata() just after your custom query

For extra info, go an read this post I have recently done


This seems, from your comments, that the problem is not related to the code in question

...I've now changed the plugin to use wp_query and added wp_reset_postdata() but the problem persists

You need to debug this, and I would look for the following

  • Any instance of pre_get_posts. One thing to remember when using pre_get_posts, it alters all queries, main and custom, if not properly used. Make sure that each instance, if it should target only the main query, that the following code is in there if( $query->is_main_query())

  • Are you maybe running a custom query in place of the main query on your category page. if so, don't do that. Please read this post which will explain everything.

  • Also, turn on debugging and check if you get any errors. Try switching to a bundled theme and deactivate all other plugins and check if your problem is related to your plugin or to the theme you are using. If you get the same issue after switching themes, it is most probably your plugin


As I suspected, it is an instance of pre_get_posts that is the culprit of the problem. Have a look at the following lines in quick-event-manager.php

Line 28: add_filter( 'pre_get_posts', 'qem_add_custom_types' );

Line 106: function qem_add_custom_types( $query ) {
Line 107:     if( is_category() || is_tag() ) {
Line 108:                   $query->set( 'post_type', array('post', 'event','nav_menu_item'));
Line 109:             return $query;
Line 110:      }
Line 111: } 

pre_get_posts don't just changes the main query, it also changes custom queries. This also don't just happens on the front end, but also in the back end. So you need to be very careful when using pre_get_posts , otherwise you'll end up with the problem you are having


The recent post widget uses a custom query to display the recent posts in the sidebar. What your code is doing, it adds the following post types to all queries when you are on either a category or tag page 'post', 'event','nav_menu_item'.

As I said, pre_get_posts acts on all queries, main and custom. That is why you see the weird behavior when on the category page, and actually, if you check, you'll have the same problem on tag pages as well. Also, you should also see weird behavior in the back end when visiting the category and tag pages.


ALWAYS when using pre_get_posts, direct it only to the main query (is_main_query()), and secondly, always check if you are on an admin page (back end) or front end page, and make changes to front end only (!is_admin())

You should change your code to

function qem_add_custom_types( $query ) {
    if( !is_admin() && $query->is_category() || $query->is_tag() && $query->is_main_query() ) {
            $query->set( 'post_type', array('post', 'event','nav_menu_item'));
        return $query;

This should take care of this problem.

  • Thanks Pieter, I've now changed the plugin to use wp_query and added wp_reset_postdata() but the problem persists. As you suggest it looks like the query isn't getting reset.
    – fisicx
    Sep 9, 2014 at 19:55
  • Sorry for the late reply, quickly did this answer before going to bed last night :-) Please see my edit. Let me know. Sep 10, 2014 at 4:41
  • Not using pre_get_posts so that's not the issue. See my edit to the OP
    – fisicx
    Sep 10, 2014 at 10:51
  • Have you tried the last point in my edit, what was you findings Sep 10, 2014 at 10:59
  • All tried except debugging. Busy this afternoon to will have a go later
    – fisicx
    Sep 10, 2014 at 11:31

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.