【问题标题】:WordPress - User Query - Order by Meta ValueWordPress - 用户查询 - 按元值排序
【发布时间】:2013-07-21 03:42:20
【问题描述】:

我正在尝试使用 WordPress 用户查询来创建按自定义元值排序的用户列表。它是一个简单的数值,从 1 到 100,因此需要首先显示 1,最后显示 100,等等。

这是我的尝试,失败得很惨:

<?php

    $args  = array(
        'role' => 'Author',
        'meta_key' => 'order-number',
        'orderby' => 'order-number',
        'order' => 'asc',  
    );

$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();

if (!empty($authors))
{
    echo '<div style="float:left;">';
    foreach ($authors as $author)
    {
        $author_info = get_userdata($author->ID);
        echo '<div class="post team-member"><div class="image">';
        echo get_avatar( $author_info->ID, 91 );
        echo '</div>';
        echo '<div class="content"><div class="title"><a href="/author/' . $author_info->user_login . '">' . $author_info->user_firstname . ' ' . $author_info->user_lastname . '</a></div>';
        echo '' . substr( get_the_author_meta('user_description',$author_info->ID) , 0 , 100 ) . '...'; 
        echo '</div></div>';
    }
    echo '</div>';
} else {
    echo 'No authors found';
}

    ?>

我认为问题在于 wp_user_query 不支持 orderby 中的自定义字段 - 所以我需要一个解决这个问题的解决方案。

有什么想法吗?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    正确。 orderby 参数只能接受可能的值 'login'(默认)、'nicename'、'email'、'url' 和 'registered'。

    一个肮脏的修复应该是这样的:

    <?php
    global $wpdb; //Ignore this line if you're not within a function
    $order = $wpdb->get_results("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key='order-number' ORDER BY meta_value ASC", "ARRAY_N");
    $authors = array();
    foreach($order as $aid)
        $authors[] = new WP_User($aid[0]);
    
    if (!empty($authors))
    {
        echo '<div style="float:left;">';
        foreach ($authors as $author)
        {
            $author_info = get_userdata($author->ID);
            echo '<div class="post team-member"><div class="image">';
            echo get_avatar( $author_info->ID, 91 );
            echo '</div>';
            echo '<div class="content"><div class="title"><a href="/author/' . $author_info->user_login . '">' . $author_info->user_firstname . ' ' . $author_info->user_lastname . '</a></div>';
            echo '' . substr( get_the_author_meta('user_description',$author_info->ID) , 0 , 100 ) . '...'; 
            echo '</div></div>';
        }
        echo '</div>';
    } else {
        echo 'No authors found';
    }
    

    这在很大程度上是未经测试的,所以我不能保证它会直接工作,但它应该让你开始。

    希望这会有所帮助!

    【讨论】:

    • @maiorano84 非常感谢,我花了好几个小时试图解决这个问题,这已经成功了。我仍在努力解决的一个问题是将其限制为仅具有“订阅者”角色的用户?
    • @tbwcf - 最好在实例化 WP_User 对象后处理该信息。要将 Author 数组限制为仅订阅者,您可以执行以下操作:foreach($order as $aid){ $u = new WP_User($aid[0]); if(in_array("subscriber", $u-&gt;roles)) $authors[] = $u; }
    • @maiorano84 太棒了,谢谢你,我真的很感激!
    【解决方案2】:
    <?php
    
    // prepare arguments this is your query.
    $args = array(
    'meta_key' => 'last_name',
    'query_id' => 'wps_last_name',
    );
    
    // Create the WP_User_Query object
    $author_query = new WP_User_Query( $args );
    
    ?>
    
    Add following lines to functions.php file
    
    <?php
    
    add_action( 'pre_user_query', 'wps_pre_user_query' );
    /*
    * Modify the WP_User_Query appropriately
    *
    * Checks for the proper query to modify and changes the default user_login for $wpdb->usermeta.meta_value
    *
    * @param WP_User_Query Object $query User Query object before query is executed
    */
    function wps_pre_user_query( &$query ) {
    global $wpdb;
    if ( isset( $query->query_vars['query_id'] ) && 'wps_last_name' == $query->query_vars['query_id'] )
    $query->query_orderby = str_replace( 'user_login', "$wpdb->usermeta.meta_value", $query->query_orderby );
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2013-02-27
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多