2013-11-04 39 views
3

我有兩個搜索查詢。用默認方式搜索與參數匹配的任何帖子標題。如何結合這兩個WordPress搜索查詢?

第二個查詢設置爲使用搜索查詢的搜索關鍵字「SKU」搜索任何帖子。

我想結合這兩個查詢,以便搜索將返回標題OR SKU匹配搜索項的任何帖子。

首先查詢:

$args = array(
      's'      => apply_filters('yith_wcas_ajax_search_products_search_query', $search_keyword), 
      'post_type'    => 'product', 
      'post_status'   => 'publish', 
      'ignore_sticky_posts' => 1, 
      'orderby'    => $ordering_args['orderby'], 
      'order'     => $ordering_args['order'], 
      'posts_per_page'  => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')), 
      'meta_query'   => array(
       array(
        'key'   => '_visibility', 
        'value'   => array('catalog', 'visible'), 
        'compare'  => 'IN' 
       ) 
      ) 
     ); 

第二個查詢:

 $args = array(
     'post_type'    => 'product', 
     'post_status'   => 'publish', 
     'ignore_sticky_posts' => 1, 
     'orderby'    => $ordering_args['orderby'], 
     'order'     => $ordering_args['order'], 
     'posts_per_page'  => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')), 
     'meta_query'   => array(
      array(
       'key'   => '_visibility', 
       'value'   => array('catalog', 'visible'), 
       'compare'  => 'IN' 
      ), 
     array(
      'key'   => '_sku', 
      'value'   => apply_filters('yith_wcas_ajax_search_products_search_query', $search_keyword), 
      'compare'  => 'LIKE' 
     ) 
     ) 
    ); 

我如何結合這兩個查詢,並返回任何帖子的標題或SKU與搜索詞匹配?

+1

我很遺憾地告訴你,但你可能即將有組合這些_meta_queries_問題。看看[這](http://stackoverflow.com/questions/18966416/wordpress-query-with-sub-relations/18971049#18971049)。在你的第二個查詢中,你有兩個以'AND'關係組合的meta_query,並且你需要使用'OR'關係將這兩個條件結合到第一個查詢的條件中,這似乎不可能。 – mathielo

回答

0

我假設你將在循環中使用參數。

您可以使用循環將所有返回的post_id添加到數組中。您可以運行兩個獨立的循環,並將所有條目添加到數組中。您需要檢查重複條目,因此您最終不會打印兩次相同的文章。

所以,你會做一些喜歡 -

//First loop  
$args = array(
    'post_type'    => 'product', 
    'post_status'   => 'publish', 
    'ignore_sticky_posts' => 1, 
    'orderby'    => $ordering_args['orderby'], 
    'order'     => $ordering_args['order'], 
    'posts_per_page'  => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')), 
    'meta_query'   => array(
     array(
      'key'   => '_visibility', 
      'value'   => array('catalog', 'visible'), 
      'compare'  => 'IN' 
     ), 
    ) 
); 
while ($loop->have_posts()) : $loop->the_post(); 
    $post_id = get_the_ID(); 
    $my_post = my_post_function($post_id); 
    //Store the items in an array 
    $my_post_array [] = $my_post; 
    query_posts($args); 
    endwhile; 

//Second loop 
    $args = array(
    'post_type'    => 'product', 
    'post_status'   => 'publish', 
    'ignore_sticky_posts' => 1, 
    'orderby'    => $ordering_args['orderby'], 
    'order'     => $ordering_args['order'], 
    'posts_per_page'  => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')), 
    'meta_query'   => array(
    array(
     'key'   => '_sku', 
     'value'   => apply_filters('yith_wcas_ajax_search_products_search_query', $search_keyword), 
     'compare'  => 'LIKE' 
    ) 
    ) 
); 
while ($loop->have_posts()) : $loop->the_post(); 
    $post_id = get_the_ID(); 
    $my_post = my_post_function($post_id); 
    //Store the items in an array 
    $my_post_array [] = $my_post; 
    query_posts($args); 
    endwhile; 

//Remove duplicate entries from the array 
array_unique ($my_post_array, SORT_STRING); 
+0

我沒有完全做到這一點,但我用它作爲一個偉大的基地,以獲得我所需要的。非常感謝您的意見! – JROB

+0

很高興幫助! –