Wordpress:按自定义帖子类型的列默认排序

时间:2022-10-02 07:44:42

I have a custom post type called Contact, with custom fields like first name, surname, telephone number etc.

我有一个名为Contact的自定义帖子类型,其中包含姓名,姓氏,电话号码等自定义字段。

In the admin section they're sorted chronologically I think, but I need them to be sorted by surname by default.

在管理部分,他们按时间顺序排序,我认为,但我需要它们默认按姓氏排序。

I've read all the other solutions on here and none of them work, including:

我在这里阅读了所有其他解决方案,但没有一个能够工作,包括:

function set_post_order_in_admin( $wp_query ) {
global $pagenow;
  if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {
    $wp_query->set( 'orderby', 'surname' );
    $wp_query->set( 'order', 'ASC' );
  }
}
add_filter('pre_get_posts', 'set_post_order_in_admin' );

But whatever field I try to sort by, nothing changes, except toggling ASC/DESC seems to change to reverse chronological ordering.

但无论我尝试排序的哪个领域,都没有任何变化,除了切换ASC / DESC似乎改变为反向按时间顺序排序。

What am I doing wrong?

我究竟做错了什么?

3 个解决方案

#1


5  

Refer below solutions,

参考以下解决方案

function wpa84258_admin_posts_sort_last_name( $query ){
    global $pagenow;
    if( is_admin()
        && 'edit.php' == $pagenow
        && !isset( $_GET['orderby'] )
        && !isset( $_GET['post_type'] ) ){
            $query->set( 'meta_key', 'last_name' );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'wpa84258_admin_posts_sort_last_name' );

OR refer this solution

或参考此解决方案

#2


3  

Replace

更换

 $wp_query->set( 'orderby', 'surname' );
 $wp_query->set( 'order', 'ASC' );

With

$query->set( 'meta_key', 'surname' ); // name of your post meta key
$query->set( 'orderby',  'meta_value'); // meta_value since it is a string

It may help

它可能有所帮助

#3


0  

I am a drupal developer and haven't got a chance to play with WordPress. We had the same problem and this is how we fixed it.

我是一个drupal开发人员,没有机会玩WordPress。我们遇到了同样的问题,这就是我们修复它的方法。

Get your custom content type data (either WP default api or custom query), it would be an array of objects. Sort them using below function. return sorted array of posts. Not sure, in which hook you need to implement this in wordpress.

获取自定义内容类型数据(WP默认api或自定义查询),它将是一个对象数组。使用以下功能对它们排序。返回已排序的帖子数组。不确定,你需要在wordpress中实现这个钩子。

/**
 *  Function to sort array by key
 *  sortArrayByKey($yourArray,"name",true); //String sort (ascending order)
 *  sortArrayByKey($yourArray,"name",true,false); //String sort (descending order)
 *  sortArrayByKey($yourArray,"id"); //number sort (ascending order)
 *  sortArrayByKey($yourArray,"count",false,false); //number sort (descending order)
 */

function sortArrayByKey(&$array, $key, $string = false, $asc = true)
{
    if ($string) {
        usort($array, function ($a, $b) use (&$key, &$asc) {
            if ($asc) return strcmp(strtolower($a{$key}), strtolower($b{$key}));
            else        return strcmp(strtolower($b{$key}), strtolower($a{$key}));
        });
    } else {
        usort($array, function ($a, $b) use (&$key, &$asc) {
            if ($a[$key] == $b{$key}) {
                return 0;
            }
            if ($asc) return ($a{$key} < $b{$key}) ? -1 : 1;
            else     return ($a{$key} > $b{$key}) ? -1 : 1;

        });
    }
}

and then in your hook, you can call this function by

然后在你的钩子里,你可以调用这个函数

 return $this->sortArrayByKey($posts, "surname");

Function copied from this answer: https://*.com/a/39872303/3086531

从这个答案复制的功能:https://*.com/a/39872303/3086531

#1


5  

Refer below solutions,

参考以下解决方案

function wpa84258_admin_posts_sort_last_name( $query ){
    global $pagenow;
    if( is_admin()
        && 'edit.php' == $pagenow
        && !isset( $_GET['orderby'] )
        && !isset( $_GET['post_type'] ) ){
            $query->set( 'meta_key', 'last_name' );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'wpa84258_admin_posts_sort_last_name' );

OR refer this solution

或参考此解决方案

#2


3  

Replace

更换

 $wp_query->set( 'orderby', 'surname' );
 $wp_query->set( 'order', 'ASC' );

With

$query->set( 'meta_key', 'surname' ); // name of your post meta key
$query->set( 'orderby',  'meta_value'); // meta_value since it is a string

It may help

它可能有所帮助

#3


0  

I am a drupal developer and haven't got a chance to play with WordPress. We had the same problem and this is how we fixed it.

我是一个drupal开发人员,没有机会玩WordPress。我们遇到了同样的问题,这就是我们修复它的方法。

Get your custom content type data (either WP default api or custom query), it would be an array of objects. Sort them using below function. return sorted array of posts. Not sure, in which hook you need to implement this in wordpress.

获取自定义内容类型数据(WP默认api或自定义查询),它将是一个对象数组。使用以下功能对它们排序。返回已排序的帖子数组。不确定,你需要在wordpress中实现这个钩子。

/**
 *  Function to sort array by key
 *  sortArrayByKey($yourArray,"name",true); //String sort (ascending order)
 *  sortArrayByKey($yourArray,"name",true,false); //String sort (descending order)
 *  sortArrayByKey($yourArray,"id"); //number sort (ascending order)
 *  sortArrayByKey($yourArray,"count",false,false); //number sort (descending order)
 */

function sortArrayByKey(&$array, $key, $string = false, $asc = true)
{
    if ($string) {
        usort($array, function ($a, $b) use (&$key, &$asc) {
            if ($asc) return strcmp(strtolower($a{$key}), strtolower($b{$key}));
            else        return strcmp(strtolower($b{$key}), strtolower($a{$key}));
        });
    } else {
        usort($array, function ($a, $b) use (&$key, &$asc) {
            if ($a[$key] == $b{$key}) {
                return 0;
            }
            if ($asc) return ($a{$key} < $b{$key}) ? -1 : 1;
            else     return ($a{$key} > $b{$key}) ? -1 : 1;

        });
    }
}

and then in your hook, you can call this function by

然后在你的钩子里,你可以调用这个函数

 return $this->sortArrayByKey($posts, "surname");

Function copied from this answer: https://*.com/a/39872303/3086531

从这个答案复制的功能:https://*.com/a/39872303/3086531