WordPress列表作者按名字或第二名搜索


WordPress list authors search by first or second name

我们的想法是制作一个搜索页面,列出网站(博客或类似网站)的作者,搜索关键字将是作者的名字或姓氏。

据我所知,没有任何WordPress功能允许根据名字和姓氏查询作者。

您需要使用WP_User_Querymeta_query参数

codex在这里有一个搜索名字和姓氏的例子:https://codex.wordpress.org/Class_Reference/WP_User_Query#Examples

相关代码:

// The search term
$search_term = 'Ross';
// WP_User_Query arguments
$args = array (
    'order' => 'ASC',
    'orderby' => 'display_name',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'first_name',
            'value'   => $search_term,
            'compare' => 'LIKE'
        ),
        array(
            'key'     => 'last_name',
            'value'   => $search_term,
            'compare' => 'LIKE'
        ),
    )
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);
// Get the results
$authors = $wp_user_query->get_results();
// Check for results
if (!empty($authors)) {
    echo '<ul>';
    // loop trough each author
    foreach ($authors as $author)
    {
        // get all the user's data
        $author_info = get_userdata($author->ID);
        echo '<li>'.$author_info->first_name.' '.$author_info->last_name.'</li>';
    }
    echo '</ul>';
} else {
    echo 'No authors found';
}