Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Created March 1, 2012 14:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GaryJones/1950278 to your computer and use it in GitHub Desktop.
Save GaryJones/1950278 to your computer and use it in GitHub Desktop.
Fix category query tweak + sticky posts + is_home issue.
<?php
add_filter( 'posts_results','child_query_is_home' );
/**
* Fixes an issue where a query that targets a taxonomy stops sticky posts from working
* on is_home.
*
* The code suggests that sticky posts should only be on the blog archive (is_home) page.
* However, if that query is amended with an inclusive cat, tag or tax restriction, then
* WP doesn't count the query as being the blog archive page anymore.
*
* In wp-includes/query.php, for each tax query that is not a "NOT IN" operator, the
* relevant is_category, is_tag, or is_tax property is set to true. This in turn means
* that is_archive is set to true, and this means is_home is set to false. Since the
* rearrangements of sticky posts requires is_home to be true, sticky posts aren't
* moved to the top under these circumstances.
*
* This function doesn't actually filter anything, but uses the raw results filter hook
* to jump in and set is_home back to true on the global $wp_query so that sticky posts
* start appearing first again. If you have multiple queries on a page, you may want to
* set conditions under which this tweak is made.
*
* @author Gary Jones
* @link https://gist.github.com/1950278
*
* @global WP_Query $wp_query Posts query object.
*
* @param array $args Raw results array of posts and current WP_Query object.
*
* @return array Original untouched $args.
*/
function child_query_is_home( array $args ) {
global $wp_query;
$wp_query->is_home = true;
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment