why pre_get_posts works fine in post type archive, but not in search result list?

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

WP Query – order posts by meta field first and then order the rest

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

Woocommerce – exclude only older out-of-stock items

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

cannot filter after using pre_get_posts

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

How to ignore posts in pre_get_posts?

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

Woocommerce: Complex query in pre_get_posts [closed]

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

Query multiple taxonomies with pre get posts

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

Check post_date in pre_get_posts

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)