【问题标题】:WordPress list authors search by first or second nameWordPress 列表作者按名字或名字搜索
【发布时间】:2016-01-31 01:01:19
【问题描述】:

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

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

【问题讨论】:

    标签: php mysql wordpress wordpress-theming backend


    【解决方案1】:

    需要使用WP_User_Querymeta_query参数

    法典在这里有一个搜索名字和姓氏的例子: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';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 2018-09-02
      • 2022-02-27
      • 2018-06-22
      相关资源
      最近更新 更多