John Mc Murray
John Mc Murray

Reputation: 363

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:

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?

Upvotes: 6

Views: 6064

Answers (3)

Themesfa
Themesfa

Reputation: 164

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}";
    }
}

Upvotes: 1

h.aittamaa
h.aittamaa

Reputation: 359

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

Upvotes: 1

Anand Choudhary
Anand Choudhary

Reputation: 565

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;
   } ?>

Upvotes: 4

Related Questions