merge two query arguments into one WP_Query call

I'm overriding WooCommerce's default search query to include searching by SKU as well. default search query

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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

What 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

相关推荐

  • merge two query arguments into one WP_Query call

    I'm overriding WooCommerce's default search query to include searching by SKU as well. default search query

    20小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信