So one of my very few customizations to this WordPress template affects the heading of the “Keyboard Review” list that you see on the right. If you actually click on one of those links, you’ll get shown the review for the keyboard, but you’ll also notice that the heading becomes “More Keyboard Reviews”.
This isn’t exactly rocket science. The code that does this is:

post; ?>

Although in_category is meant to be used while in The Loop, I cheat by setting the $post variable because that’s what in_category checks against. This is all fine and dandy, and this trick is actually documented on the WordPress site.
However, after the heading, I have two lists of posts: one for the keyboard reviews, and another for my “review collections.” I had trouble for a long time, because for some reason, if I changed the order of these two listsings (i.e. put the review collections listing first), then my little trick above would stop working.
I finally figured out the source of the problem after a bit. The standard way to generate lists such as these is by doing something like:

$posts = get_posts('some condition');
foreach ($posts as $post):
  ?> ... <?

Ah ha, each time I simulate “The Loop” after performing a fake query. From experimentation, it appears that this overwrites the $wp_query variable, which makes my first trick work incorrectly. The solution? just cache the value of $wp_query before you call get_posts() anywhere, and you should be good to go.
Wordpress seems to have a lot of magic global variables like this, with API functions that have magical, non-obvious side-effects.

Leave a comment

Your email address will not be published. Required fields are marked *