How to do pre_get_posts action?
-
I get a warning
PHP Warning: Creating default object from empty value
from the line starts with @ below.
add_action( 'pre_get_posts', 'bv_query_exclude_private'); function bv_query_exclude_private(&$query){ if(!bv_member_privilege() && property_exists($query,'tax_query') ){ $tax_query= array( 'taxonomy' => 'category', 'include_children' => true, 'field' => 'term_id', 'operator' => 'NOT IN', 'terms' => array( 1 ), ); @$query->tax_query->queries[] = $tax_query; $query->query_vars['tax_query'] = $query->tax_query->queries; } }
After some digging, it appears that I am using un-initialised $query and I should define
$query = new WP_Query( $args )
within the function. Have I understood something wrong? How can I change the query to exclude term_id 1 in category?Best regards,
Alan
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How to do pre_get_posts action?’ is closed to new replies.