2015-01-14 130 views
2

請幫我按標題和meta, 例如搜索創建查詢參數:
項目:
1 - 標題:公司,內容:航空自衛隊,meta_field_vendor:ASD
2 - 標題:補償,內容:公司,meta_field_vendor:ASD
3 - 標題:ttcmp,內容:航空自衛隊,meta_field_vendor:ASD
4 - 標題:myrus,內容:航空自衛隊,meta_field_vendor:公司

?我的搜索字符串s =公司
搜索查詢參數

我想搜索結果項:1,2,4

這qyuery ARG

$args['wp_query'] = array(
    'post_type' => $post_type, 
    'posts_per_page' => 5, 
    's' => $search_s, 
); 

結果1和2

這qyuery arg

$args['wp_query'] = array(
    'post_type' => $post_type, 
    'posts_per_page' => 5, 
    'meta_query' => array (
     array(
      'key' => '_item_prop_title', 
      'value' => $search_s, 
      'compare' => 'EXISTS' 
     ) 
    ) 
); 

結果4

我該如何查詢結果1,3,4?

$args['wp_query'] = array(
    'post_type' => $post_type, 
    'posts_per_page' => 5, 
    's' => $search_s, 
    'meta_query' => array (
     array(
      'key' => '_item_prop_title', 
      'value' => $search_s, 
      'compare' => 'EXISTS' 
     ) 
    ) 
); 

乾杯!

回答

1

你可以爲你的問題做的是搜索的雙重過程中,如果它不是一個最佳的反應,但如果它工作...

$search_s = 'mykeyword'; 

    $q1 = get_posts(array(
     'post_type' => 'post', 
     's' => $search_s 
    )); 

    $q2 = get_posts(array(
      'post_type' => 'post', 
      'meta_query' => array(
       array(
        'key' => 'my_meta_box', 
        'value' => $search_s, 
        'compare' => 'LIKE' 
       ) 
      ) 
    )); 

    $merged = array_merge($q1, $q2); 

    $post_ids = array(); 
    foreach($merged as $item) { 
     $post_ids[] = $item->ID; 
    } 

    $unique = array_unique($post_ids); 

    $posts = get_posts(array(
     'post_type' => 'post', 
     'post__in' => $unique, 
     'post_status' => 'publish', 
     'posts_per_page' => -1 
    )); 

    if($posts) : foreach($posts as $post) : 
     setup_postdata($post); 

     the_title(); 


    endforeach; 
    endif; 
+0

我遵循你的建議,它的工作原理。非常感謝你。 – SokoLBY

0

您是否嘗試過類似?:

$args['wp_query'] = array(
    'post_type' => $post_type, 
    'posts_per_page' => 5, 
    's' => $search_s, 
    'meta_query' => array(
     array(
      'key'  => '_item_prop_title',// Name of the key you want to search. Check your BD to be sure this is the correct name 
      'value'  => $search_s, 
      'compare' => 'LIKE',// You can use '=' instead if you want to be more restrictive. 
     ), 
    ), 
);