I am calling this code from my custom sidebar, in order to get related postings from the same category:

   'cat' => get_category_id(),
   'showposts' => 5,
   'post__not_in' => array(get_the_ID())

get_template_part('article-list'); // outputs the posts

This works. The have_posts()-loop inside of my article-list.php now only outputs 5 other postings from the category.

But this also causes a problem: No matter on what kind of page I am (single.php, index.php, ...), WordPress always assumes that I am in the category template. So is_category() always returns true after calling query_posts('cat=...'). Thus is_single() returns false, although the single.php-template is used.

What can I do to tell WordPress that it is still using the single-template? Or: What other function can I use to detect which (main) view is loaded?

  • As you are resetting the main query... simply use new WP_Query(), get_posts() or one of the filters to alter the statement: pre_get_posts, posts_clauses (or similar). In short: There is (close to) no reason to ever use query_posts(). For more information about what happens, please refer to the answers at the linked question. Thanks.
    – kaiser
    Commented May 5, 2014 at 14:20
  • 1
    I didn't even know that query_posts is that bad. Thanks, the other thread was really helpful.
    – YMMD
    Commented May 5, 2014 at 14:40


Browse other questions tagged or ask your own question.