Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

Intercepting the Wordpress Loop with Pre_get_posts


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;

Tagged w/ #filters #is_home #query #wordpressprogramming