[wp-hackers] exclude category from main loops via plugin
Paul
paul at codehooligans.com
Fri Dec 17 15:33:03 UTC 2010
On Dec 17, 2010, at 10:05 AM, Jeremy Clarke wrote:
> On Mon, Dec 13, 2010 at 8:17 AM, Erick Hitter <ehitter at gmail.com> wrote:
>
> function gv_query_manipulation_parse_request() {
>
> add_action('pre_get_posts', 'gv_query_manipulation_pre_get_posts');
> }
> add_action('parse_request', 'gv_query_manipulation_parse_request');
>
> function gv_query_manipulation_pre_get_posts($wp_query){
> global $excluded_category;
>
> remove_action('pre_get_posts', 'gv_query_manipulation_pre_get_posts');
>
> if (is_admin() OR is_feed())
> return;
>
> $wp_query->query_vars['category__not_in'][] = $excluded_category;
>
> return;
> }
>
> Note that I've explicitly excluded is_admin and is_feed from being affected.
> You should consider these other situations where pre_get_posts takes effect
> and add negative checks as necessary.
>
Jeremy,
In your code you are calling the function is_feed(). Isn't this wrong?
When you call the is_* functions (exluding is_admin()) like is_home(), is_search(), is_archive() etc. they check the setting of the global $wp_query object. But this is not always the correct instance of the WP_Query object being queried. In other words some other part of the code may create a new instance.
Instead the better solution (IMHO) is to check the $query object like:
if ($query->is_feed) {
}
if ($query->is_home) {
}
if ($query->is_archive) {
}
if ($query->is_single) {
}
More information about the wp-hackers
mailing list