Slow WP_Query for custom post type

That’s a known problem with WordPress and searches involving postmeta. What I’d try to do is to create a new taxonomy for “things” you’re usually searching the most: offer_order_status seems the better candidate, as it can be active or not. I’d create a taxonomy with two elements. That would reduce the time of your search … Read more

Get all products which have both product category

Set include_children to false in the tax_query. $args = array( ‘post_type’ => array( ‘product’, ‘product_variation’ ), ‘tax_query’ => array( array( ‘taxonomy’ => ‘product_cat’, ‘field’ => ‘slug’, ‘terms’ => array( ‘shop’, ‘cat1’ ), ‘include_children’ => false, ‘operator’ => ‘AND’, ) ) ); See https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

query_vars filter not working even though query string parameter is present

I’m not sure it quite works like that. Try inspecting $query->public_query_vars instead and I think you’ll see it added in there. The way I usually use it is like this: add_filter( ‘query_vars’, ‘add_test_query_vars’); function add_test_query_vars($vars){ $vars[] = “test”; return $vars; } So the same as you but with a named function. Then I add a … Read more

Order By table field comment_status in WordPress > 4.0

Here’s a way to add an extra Comment Status field to the posts list table: add_filter(‘manage_post_posts_columns’, function ( $columns ) { $_columns = []; foreach( (array) $columns as $key => $label ) { $_columns[$key] = $label; if( ‘title’ === $key ) $_columns[‘wpse_comment_status’] = esc_html__( ‘Comment Status’, ‘mydomain’ ); } return $_columns; } ); add_action( ‘manage_post_posts_custom_column’, … Read more

SQL: What is wrong with the following query (generated by WordPress WP_Query, ordering prices)

Solved. After the edit it was easy. It is allowed to use type => ‘decimal(x,x)’ in meta_query. Maximum precision is decimal(65,30). https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html array(6) { [“orderby”]=> array(2) { [“uss_product_price”]=> string(4) “DESC” [“title”]=> string(3) “ASC” } [“meta_query”]=> array(3) { [“relation”]=> string(3) “AND” [“sortprimary_clause”]=> array(3) { [“key”]=> string(17) “price” [“compare”]=> string(6) “EXISTS” [“type”]=> string(13) “decimal(30,5)” } [0]=> array(4) … Read more

add_filter() depending on search term

The pre_get_posts action runs before the posts_join and posts_where filters. So, pre_get_posts is a suitable hook for conditionally adding callbacks to the posts_join and posts_where filters: function wpse249060_pre_get_posts( $query ) { // Make sure we’re doing a search query and that the search // term is NOT ‘something’ if ( $query->is_search() && $query->query_vars[‘s’] !== ‘something’ … Read more

Return only post(s) which have post_excerpt

Something along these lines should work, not tested for syntax errors though function random_post() { $args = array( ‘post_type’ => ‘post’, ‘orderby’ => ‘rand’, ‘posts_per_page’ => 1, ); $post = query_posts( $args ); } if(!$post->post_excerpt){ random_post(); } // Then down here you would do whatever with the $post object