6

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?

John Mc Murray
  • 363
  • 5
  • 17
  • Use this awesome plugins https://wordpress.org/plugins/codepress-admin-columns/ – Anand Choudhary May 10 '18 at 06:16
  • 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. – Sally CJ May 13 '18 at 11:15
  • 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. – John Mc Murray May 17 '18 at 10:13
  • 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. – Sally CJ May 18 '18 at 04:32
  • @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. – Mukesh Panchal May 18 '18 at 06:29

3 Answers3

4

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;
   } ?>
brasofilo
  • 25,496
  • 15
  • 91
  • 179
Anand Choudhary
  • 566
  • 4
  • 16
  • Thank you Anand, but my code already does that and I do mention that I'm not getting the value from post_meta... – John Mc Murray May 10 '18 at 07:43
  • 1
    Hi Anand, Stack Snippets are only for JS/HTML/CSS, please check: [Runnable code snippets in questions and answers](https://meta.stackoverflow.com/q/269753/1287812) – brasofilo May 15 '18 at 15:08
1

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, ...

h.aittamaa
  • 329
  • 3
  • 4
  • 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 – John Mc Murray May 17 '18 at 10:08
1

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}";
    }
}
Themesfa
  • 164
  • 1
  • 8