Tech moves quick and this article was published over a year ago. It might be out of date by now.

Intercepting the Wordpress Loop with Pre_get_posts

Written by Sean Behan on April 22nd 2010

#deprecated (see http://codex.wordpress.org/Function_Reference/query_posts#Post_.26_Page_Parameters)

Using the “pre_get_posts” filter you can intercept the Wordpress loop and override or set attributes in the “$query” object. This is useful because you can select just the posts you want, say for instance on your homepage you want to show posts from just one category.

add_filter(‘pre_get_posts’, ‘filter_homepage_posts’);
function filter_homepage_posts($query) {
$limit_number_of_posts = 5;
$featured_category_id = get_cat_id(‘Reviews’); // by cat name…
if ($query->is_home) {
$query->set(‘cat’, $featured_category_id);
$query->set(‘showposts’, $limit_number_of_posts);
}
return $query;
}


comments powered by Disqus