排序Wordpress时在用户表中添加的自定义列的分页

时间:2015-05-21 05:39:10

标签: php wordpress sorting pagination

在Wordpress的用户表中添加了名为company的自定义列。此外,还对该列的功能进行了排序。但是,当使用该自定义列对表进行排序时,分页不会显示。

if ('company' == $query->query_vars['orderby']) {
    $query->query_fields = 'u.*,um.*,c1.*';
    $query->query_from = 'FROM '. $wpdb->prefix.'users as u JOIN '. $wpdb->prefix . 'usermeta as um ON u.ID=um.user_id JOIN ' . $wpdb->prefix . 'company as c1 ON um.meta_value=c1.company_id';
    $query->query_orderby = ' ORDER BY company_name ' . ($query->query_vars["order"] == "ASC" ? "asc " : "desc "); 
    $query->query_limit='LIMIT 20';
}

另外,如果我们像这样点击第2页的网址

http://example.com/wp-admin/users.php?orderby=company&order=asc&paged=2

它会重定向到

http://example.com/wp-admin/users.php?orderby=company&order=asc&paged=1

1 个答案:

答案 0 :(得分:0)

我明白了

 if ('company' == $query->query_vars['orderby']) {
$limit =20;
        if(isset($_GET['paged'])){
            $page = $_GET['paged'];
            $offset = ($page - 1)  * $limit;
            $start = $offset + 1;
        }else{
            $start = 1;
        }
        if($_GET['paged'] == 1){
            $start = 1;
        }
        $query->query_fields = 'u.ID';
        $query->query_from = 'FROM '. $wpdb->prefix.'users as u JOIN '. $wpdb->prefix . 'usermeta as um ON u.ID=um.user_id JOIN ' . $wpdb->prefix . 'company as c1 ON um.meta_value=c1.company_id';
        $query->query_orderby = ' ORDER BY company_name ' . ($query->query_vars["order"] == "ASC" ? "asc " : "desc "); 
      $query->query_limit="LIMIT $start, $limit";
    }

仅选择用户ID而不是全部。

   $query->query_fields = 'u.ID';

而不是

 $query->query_fields = 'u.*,um.*,c1.*';

并更改$query->query_limit=20;

to $query->query_limit="LIMIT $start, $limit";