2015-01-14 175 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. 
     ), 
    ), 
);