I'm overriding WooCommerce's default search query to include searching by SKU as well.
// default search query
$query_default_search = new WP_Query(
array(
"post_type" => "product",
"s" => $search_query
)
);
// SKU query
$query_search_by_sku = new WP_Query(
array(
"post_type" => "product",
"meta_query" => array(
array(
"key" => "_sku",
"value" => $search_query,
"compare" => "LIKE"
)
)
)
);
If the default search query doesn't return any posts, I assume that an SKU has been entered and I run the SKU query. However, I want to merge both query arguments into one WP_Query
call, rather than merging the results of both queries using array_merge
.
If both queries had meta-query, I could use an OR relation, but this is different.
How can this be done?
I'm overriding WooCommerce's default search query to include searching by SKU as well.
// default search query
$query_default_search = new WP_Query(
array(
"post_type" => "product",
"s" => $search_query
)
);
// SKU query
$query_search_by_sku = new WP_Query(
array(
"post_type" => "product",
"meta_query" => array(
array(
"key" => "_sku",
"value" => $search_query,
"compare" => "LIKE"
)
)
)
);
If the default search query doesn't return any posts, I assume that an SKU has been entered and I run the SKU query. However, I want to merge both query arguments into one WP_Query
call, rather than merging the results of both queries using array_merge
.
If both queries had meta-query, I could use an OR relation, but this is different.
How can this be done?
Share Improve this question asked Aug 9, 2015 at 16:12 adiadi 112 bronze badges2 Answers
Reset to default 1What about this ?
$query_withMeta_search = new WP_Query(
array(
"post_type" => "product",
"s" => $search_query,
"meta_query" => array(
"key" => "_sku",
"value" => $search_query,
"compare" => "LIKE"
)
)
);
Create the array you'd like to use in it's entirety before running. Something like
if($search_query) {
// create the basic query array
} else {
// create the more advanced query array
}
// then run the query on the one you want
new WP_Query($args);
Hope that helps.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744985649a4604593.html
评论列表(0条)