Sorting Custom Column in Wordpress Admin Pages

wordpress admin columns filter
wordpress custom post type admin columns
wordpress reorder admin columns
how to add custom field in wordpress post admin
wordpress custom column sortable
customizing admin columns in wordpress
woocommerce admin columns
add custom column to users admin panel

I'm trying to sort a custom column in the admin edit screen. The column is contains an integer value (vote counts).

To generate the column I use this "standard" code:

add_filter( 'manage_edit-entries_sortable_columns', 'cutify_entries_columns_sortable' );
function cutify_entries_columns_sortable( $columns ) 
{
    $columns['entry_vote_count'] = 'entry_vote_count';
    return $columns;
}

add_filter( 'manage_entries_posts_columns', 'cutify_entries_columns_head' );
function cutify_entries_columns_head($defaults) 
{
    unset( $defaults['date'] );
    $defaults['entry_vote_count'] = 'Votes';
    return $defaults;
}

add_action('manage_entries_posts_custom_column', 'cutify_entries_columns_content', 10, 2);
function cutify_entries_columns_content($column_name, $post_ID) 
{
    if ($column_name == 'entry_vote_count') 
    {
        $number = rand(1,1000);

        print intVal($number);
    }
}

The issue is trying to sort this column. I've read many answers here and on other sites and I do know about sorting this if the value came from post_meta, but as you can see, in this case the value comes from a return value from function call.

Is there any way of sorting a custom column not based on a post_meta value?

Register A Columns First thing you need to register a column

<?php 
add_action( 'manage_cake_posts_custom_column', 'my_cake_column_content', 10, 2 );
function my_cake_column_content( $column_name, $post_id ) {
    if ( 'slices' != $column_name )
        return;
    //Get number of slices from post meta
    $slices = get_post_meta($post_id, 'slices', true);
    echo intval($slices);
} ?>

Make a Column Sortable

<?php 
 add_filter( 'manage_edit-cake_sortable_columns', 
 'my_sortable_cake_column' );
 function my_sortable_cake_column( $columns ) {
 $columns['slices'] = 'slice';

  //To make a column 'un-sortable' remove it from the array
  //unset($columns['date']);

   return $columns;
   } ?>

Sorting Custom Column in Wordpress Admin Pages, Register A Columns First thing you need to register a column <?php add_action( '​manage_cake_posts_custom_column'  Sorting Custom Column in Wordpress Admin Pages. I'm trying to sort a custom column in the admin edit screen. The column is contains an integer value (vote counts). To generate the column I use this "standard" code: The issue is trying to sort this column.

The simplest way if you do not develop a plugin is to use an existing one like Admin Columns because it allows you to perform several advanced actions : orders, adding columns, ...

Sorting for custom column in WordPress admin post table , The filter name is slightly off - use manage_edit-st_kb_sortable_columns . As for the actual sorting not working, you need implement the logic yourself: function  Changing the existing columns can be accomplished using two WordPress hooks: manage_[post_type]_posts_columns, which allows you to remove, reorder and add columns, and manage_[post_type]_posts_custom_column. In place of [post_type], enter the post type you wish to target.

You must add custom query_orderby to WP_Query when wp trying to get posts order by your custom column. like this:

add_action( 'pre_get_posts', 'my_entry_vote_orderby' );
function my_entry_vote_orderby( $query ) {
    global $wpdb;

    // Only filter in the admin
    if( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby');

    // Only filter if orderby is set to 'entry_vote_count'
    if( 'entry_vote_count' == $orderby ) {
        // We need the order - default is ASC
        $order = isset( $query->query_vars ) && isset( $query->query_vars[ 'order' ] ) && strcasecmp( $query->query_vars[ 'order' ], 'desc' ) == 0 ? 'DESC' : 'ASC';

        // Order the posts by vote count
        $query->query_orderby = "ORDER BY ( SELECT vote_count FROM {$wpdb->posts} posts WHERE posts.post_type = 'post' AND posts.post_status='publish' ) {$order}";
    }
}

Sorting admin columns with meta_value, I have been working on a plugin to add custom columns to users table and then Remember it is not a post or a custom post or page but a user's admin table. The sorting feature of Admin Columns helps you to sort content on any WordPress list table. Need to know which post uses the most words? Or which images are slowing down your site due to their large size? Sorting helps you find any type of content faster and easier, reducing the time needed to manage your content.

WordPress Sortable Admin Columns for Custom Posts, Adding custom columns to the admin tables for your custom post types is a good way to provide more information to your users. WordPress  Adding custom columns to the admin tables for your custom post types is a good way to provide more information to your users. WordPress provides a flexible method to add data whether it’s meta values, taxonomies, images, ACF PRO fields, or anything else tied to your post types.

WordPress: add and sort custom column in users Admin page , php) page within the WordPress Admin. The custom column needed to show a custom author meta field. I found a few tutorials online on this, and  In a previous tutorial, I showed you how to display a plain, non-sortable “Views” Column for Posts in the WordPress Dashboard.In this tutorial, I will show you how to make that “Views” Custom Column Sortable for Posts, ascending or descending based on the number of post views.

Quick Tip: Make Your Custom Column Sortable, To tell WordPress which columns you want to register as sortable you For posts and pages, $screen->id is 'edit-post' and 'edit-page' respectively. front end as well, it's a good idea to only effect queries on the admin side. I've set up a custom post type called clientarea, and set up several custom columns for it in the admin area - the custom columns are all custom meta fields, as you can see from my code. I'd like to sort by 'Appointment Date' descending by default.

Comments
  • Use this awesome plugins wordpress.org/plugins/codepress-admin-columns
  • Can you update your question and include the code from cutify-votes/classes/class.API.php? Or you can post it on Pastebin.com and add the link to the Paste to your question.
  • Thanks Sally CJ, but the code in the class is not relevant. What is relevant is that its an integer value that gets returned. I've updated my code sample above to use a random int rather than a function call.
  • Sorry but, are you sure the class "is not relevant"? I mean, there must be a method/function in the class which saves/updates the votes count for specific posts, right? Even if the votes count isn't saved to the posts meta table.
  • @JohnMcMurray you are using rand function and it generate new number every time so when you try to sort column rand function generate new number so it will not give you perfect result so you have to store unique post vote value in post meta then it will work fine.
  • Thank you Anand, but my code already does that and I do mention that I'm not getting the value from post_meta...
  • Hi Anand, Stack Snippets are only for JS/HTML/CSS, please check: Runnable code snippets in questions and answers
  • Thank you but I cannot use a plugin for this. The column does not get its data from a meta field, its an integer return value from a function call. All of these plugins depend on built in wordpress data (postmeta, etc) and that's not what I have