wordpress自定义帖子类型按元键搜索

时间:2013-12-03 04:04:20

标签: wordpress

我有一个自定义帖子类型MEMBER,现在有一个元字段member_name我想搜索名称以'S'开头的所有成员。

普通SQL:WHERE member_name类似'S%';

如何在wordpress中完成?

我做了什么吼叫:

 $arg = array(
                        'post_type' => 'member',
                        'post_status' => 'publish',
                        'order' => 'ASC',
                        'meta_key' => 'member_name',
                        'orderby' => 'meta_value',
                        'meta_query' => array(
                                                array(
                                                     'key' => 'member_name',
                                                     'value' => $sort_by,
                                                     'compare' => 'LIKE'
                                              )
                                )
                        );
$wp_query = new WP_Query($arg);

1 个答案:

答案 0 :(得分:2)

您可以尝试以下代码:

此查询返回名称以S开头的成员。

    global $wpdb; 
         $query ="SELECT *
                FROM wp_posts as p, wp_postmeta as m
                WHERE p.ID = m.post_id AND p.post_status = 'publish' AND p.post_type = 'member' AND m.meta_key = 'member_name' AND m.meta_value LIKE 'S%' ORDER BY m.meta_value ASC";   

$results = $wpdb->get_results($query);