[wp-hackers] exclude category from main loops via plugin

Alexander Höreth a.hoereth at gmail.com
Mon Dec 13 14:18:12 UTC 2010


Am 13.12.2010 14:17, schrieb Erick Hitter:
> pre_get_posts is my preferred method. The hook fires before the
> query executes, so there are no unnecessary database calls. The hook's first
> argument is an object whose query_vars method is an array that you can add
> your category exclusion (or any other query arguments) to.
>
> Here's a sample:
> function my_query( $query ) {
> $query->query_vars[ 'category_name' ] = 'mycategory';
>   return $query;
> }
> add_action( 'pre_get_posts', 'my_query' );
>
> Erick
>
> On Mon, Dec 13, 2010 at 08:03, Alexander Höreth<a.hoereth at gmail.com>  wrote:
>
>> pre_get_posts
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
works perfectly. only using id, but doesnt matter :) thanks!

function exclude_category($query) {
         $id= get_category_by_slug($options['mycategoryslug']);
         $query->query_vars['cat'] = -$id;
}
add_action('pre_get_posts', 'exclude_category');


More information about the wp-hackers mailing list