【问题标题】:Search query parameters by title and meta按标题和元搜索查询参数
【发布时间】:2015-03-12 11:01:37
【问题描述】:

请帮我创建用于按标题和元搜索的查询参数, 示例:
项目:
1 - 标题:公司,内容:asdf,meta_field_vendor:asd
2 - 标题:comp,内容:公司,meta_field_vendor:asd
3 - 标题:ttcmp,内容:asdf,meta_field_vendor:asd
4 - 标题:myrus,内容:asdf,meta_field_vendor:公司

我的搜索字符串 ?s=company

我要搜索的结果是项目: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'
        )
    )
);

干杯!

【问题讨论】:

    标签: php search wordpress


    【解决方案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;
    

    【讨论】:

    • 我听从了你的建议,它有效。非常感谢。
    【解决方案2】:

    你有没有尝试过类似的东西?:

    $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.
            ),
        ),
    );
    

    【讨论】:

    • 这意味着两者之间的“AND”:不幸的是,只会返回标题中带有 $search_s 且元键中的帖子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 2021-12-19
    • 2015-11-06
    相关资源
    最近更新 更多