Query_posts and how to use it in your functions.php template file

Here’s a trick I learned tonight.


/* this code excludes all of the 'international (id = 20) posts AND CHILDREN */

function exclude_category($query) {
$child_cats = (array) get_term_children('20', 'category');
if ( $query->is_home ) {
$query->set('category__not_in',array_merge(array('20'), $child_cats));
return $query;
}
}
add_filter('pre_get_posts', 'exclude_category');

the $query->set is used just like query_posts
who knew?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s