Exclude category by slug in pre_get_posts?
Here’s one way with the following tax query: ‘tax_query’ => [ [ ‘taxonomy’ => ‘category’, ‘field’ => ‘slug’, ‘terms’ => [ ‘foo’ ], ‘operator’ => ‘NOT IN’ ], ], where the foo term slug is excluded.
Here’s one way with the following tax query: ‘tax_query’ => [ [ ‘taxonomy’ => ‘category’, ‘field’ => ‘slug’, ‘terms’ => [ ‘foo’ ], ‘operator’ => ‘NOT IN’ ], ], where the foo term slug is excluded.
If searching via WordPress ?s=keyword (not WooComm’s Product Search), post_type isn’t set or used, so the following would return false: if( isset($query->query_vars[‘post_type’]) && $query->query_vars[‘post_type’] == ‘product’ ) { You can explore what $query->query_vars contains on your Products page vs your Products Search page by just printing it and killing the page: function my_pre_get_posts( $query ) … Read more
You should be able to name and sort your meta query clauses like this: $meta_query = array( ‘relation’ => ‘OR’, ‘foo’ => array( ‘key’ => ‘expiring_date’, ‘value’ => date(‘Ymd’), ‘compare’ => ‘>’, ‘type’ => ‘DATE’ ), ‘bar’ => array( ‘key’ => ‘expiring_date’, ‘compare’ => ‘NOT EXISTS’ ), ‘baz’ => array( ‘key’ => ‘expiring_date’, ‘value’ => … Read more
Most information necessary can be found at the Code Reference: WP_Query | Class . Otherwise useful resource Theme Handbook: Conditional Tags. Below code contains some clarifying comments. Please note, this is untested code, so no guarantees, but it should get you going. add_action( ‘woocommerce_product_query’, ‘custom_woocommerce_product_query’ ); function custom_woocommerce_product_query( $q ) { if ( ! is_admin() … Read more
The problem is that you always set the meta query, if you don’t want it to apply in a particular situation, you need to check for that situation and account for it. Similar to how you’re already checking with is_admin, or is_main_query, etc. In this situation, you’ll want to check the $_GET parameters to detect … Read more
What about this one? function opby_query( $query ) { if ( $query->is_main_query() ) { if ( $query->is_home() ) { $query->set(‘posts_per_page’, 15); }; if ( isset( $query->query_vars[‘ctrl_podcasts_status’] ) ) { // get main query args $subquery_args = $query->query_vars; // add tax_query filter $subquery_args[‘tax_query’] = array( array( ‘taxonomy’ => ‘category’, ‘field’ => ‘slug’, ‘terms’ => array( ‘podcast-control-daily’ … Read more
When I have complex querie to do, I often use a prequery to get the good post_ids. Then I pass this list of post_ids to my main query via the post__in parameter of WP_Query. $meta_key = ‘RepeaterName_%_FieldName’; $meta_value=”MyCustomValue”; global $wpdb; $post_ids = $wpdb->get_col( $wpdb->prepare( ” SELECT pm.post_id FROM $wpdb->postmeta pm WHERE pm.meta_key LIKE %s AND … Read more
It’s because when you use the is_post_type_archive( ‘object’ ) condition you’re checking whether the main query is a post type archive, not whether this query is a post type archive. This means that your code will apply to all queries, including for menus, on the post type archive. You need to use $query->is_post_type_archive() to check … Read more
I can’t seem to find a way to set the query more than one taxonomy You already found it, but the problem is, you should have not done this: $tax_query_array = array(‘relation’ => ‘OR’); $speciality_select ? array_push($tax_query_array, …) : null ; $query->set( ‘tax_query’, $tax_query_array); $tax_query_array = array(‘relation’ => ‘OR’); $industry_select ? array_push($tax_query_array, …) : null … Read more
You can’t really check post_date in pre_get_posts, because, well, the action fires before (pre_…) the posts have actually been fetched (…get_posts). 🙂 But, you can use $query->set() to add date parameters to the query. This is taken from an example in the WP_Query() Codex entry: <?php function wpse54142_filter_pre_get_posts( $query ) { if ( is_feed() ) … Read more