WordPress - 从帖子,postmeta和usermeta查询

时间:2018-06-12 15:47:14

标签: wordpress

我有一个名为'bo_codes'的自定义帖子类型,每个帖子都有postmeta中的几个选项。 postmeta中的一个选项称为“claim_by”,是users表中的用户ID。在前端我使用'get_the_author_meta(' first_name',$ claim_by)。' ' .get_the_author_meta(' last_name',$ claims_by);'获取用户的姓名。这一切都正常工作,数据进入WP_List_Table。我正在使用WP_List_Table搜索框,并希望用户能够搜索用户的名字和姓氏。当用户进行搜索时,我使用WP_Query来获取结果。这适用于获取postmeta但我需要能够在usermeta中搜索用户的名字和姓氏,并显示相应的“bo_codes”。

这是我到目前为止的查询。

$searchvalue = $_REQUEST['s'];
        $args = array(
            'post_type'             => 'bo_codes',
            'post_status'           => 'publish',
            'posts_per_page'        => '-1',                
            'meta_query' => array(
                'relation' => 'AND', //**** Use AND or OR as per your required Where Clause 
                array(
                'relation' => 'OR',             
                    array(
                        'key'     => 'bo_code',
                        'value'   => $searchvalue,
                        'compare' => 'LIKE',
                    ),
                    array(
                        'key'     => 'run_number',
                        'value'   => $searchvalue,
                        'compare' => 'LIKE',
                    ),
                    array(
                        'key'     => 'claimed_by',
                        'value'   => $searchvalue,
                        'compare' => 'LIKE',
                    ),
                ),
                array(
                    'key'     => 'status',
                    'value'   => 'Claimed',
                    'compare' => '=',
                ),
            ),  

        );

$result = new WP_Query($args);
    $data = array_map(
        function( $post ) {
            return (array) $post;
        },
        $result->posts
    );

我应该使用'post_join'操作还是绕过WP_Query并使用'$ wpdb-> get_results'和一个使用'LEFT JOIN'的特殊查询?

以下是我尝试使用get_results的内容。

global $wpdb;
    $query = "  
        SELECT $wpdb->posts.ID,
        first_name.meta_value AS first_name,
        last_name.meta_value AS last_name
       FROM $wpdb->posts

  LEFT JOIN wp_usermeta  AS first_name 
         ON $wpdb->posts.ID = first_name.post_id AND first_name.meta_key='first_name'

  LEFT JOIN wp_usermeta  AS last_name
         ON $wpdb->posts.ID = last_name.post_id AND last_name.meta_key='last_name'

      WHERE $wpdb->posts.post_status = 'publish'
      AND wp_posts.post_type = 'bo_codes'
        ";

$data = $wpdb->get_results( $query, ARRAY_A );

我花了很多时间试图绕过这个,但我还没到那里。

1 个答案:

答案 0 :(得分:0)

我知道了。

$query = "
            SELECT $wpdb->posts.ID, cu.meta_value as customer, bc.meta_value as the_bo_code, st.meta_value as status, au.meta_value as author, rn.meta_value as run_number                
            FROM $wpdb->posts
            LEFT JOIN $wpdb->postmeta AS cu ON ($wpdb->posts.ID = cu.post_id AND cu.meta_key='author')
            LEFT JOIN $wpdb->postmeta AS bc ON ($wpdb->posts.ID = bc.post_id AND bc.meta_key='the_bo_code')                
            LEFT JOIN $wpdb->postmeta AS st ON ($wpdb->posts.ID = st.post_id AND st.meta_key='status')
            LEFT JOIN $wpdb->usermeta AS au ON (cu.meta_value = au.user_id AND au.meta_key='nickname')
            LEFT JOIN $wpdb->postmeta AS rn ON ($wpdb->posts.ID = rn.post_id AND rn.meta_key='run_number')
            WHERE $wpdb->posts.post_type = 'bo_code_run'
            AND $wpdb->posts.post_status = 'publish'
            GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.post_date DESC LIMIT 50;
        ";
$data = $wpdb->get_results( $query, ARRAY_A );