按标题和元搜索查询参数

时间:2015-01-14 09:47:41

标签: php wordpress search wp-query

请帮我创建按标题和元搜索的查询参数, 例如:
项目:
 1 - 标题:公司,内容:asdf,meta_field_vendor:asd
 2 - title:comp, content:company ,meta_field_vendor:asd
 3 - title:ttcmp,content:asdf,meta_field_vendor:asd
 4 - 标题:myrus,内容:asdf, 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'
        )
    )
);

干杯!

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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.
        ),
    ),
);