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

WordPress list authors search by first or second name

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

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

您需要使用WP_User_Query

meta_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';
}