Expert Group of programming
Loading...

How To Make Your WordPress Admin Columns Sortable

Posted by : Heycode Tech | Posted in : admin customization, wordpress | Dec 6,2015 | 1 comments

Sometimes,we need to sort the posts listing(In admin dashboard) by desired custom key.For implementing the sorting of the posts,we need to write the code.This article is describing about it i.e how to develop the sorting of post listing .

As we know that wordpress also providing the sorting(By default) of posts by –
i : Post title.
ii: Post date (Recently posts visible on top- Default active Feature).

But here I ‘m describing the sorting of posts by custom key.

sorting-cpt-list

Here is the steps for registering the custom key on post listing and sorting the posts.
Step 1 : Registering the custom key.
Step 2 : Implementing the sorting code.

Step 1 : Registering the custom key
Suppose our custom post type : wp_study :,Now I can use this sorting for default posts section also.

add_filter('manage_wp_study_posts_columns', 'admin_wp_study_custom_columns');
function admin_wp_study_custom_columns( $posts_columns ) {
    $posts_columns = array(
        '_custom_id' => __( 'Custom ID' ) // custom id meta key
    );
    return $posts_columns;
}

// Fill the column with the appropriate items
add_action( 'manage_wp_study_posts_custom_column', 'manage_wp_study_columns', 10, 2 );
function manage_wp_study_columns( $column, $post_id ) {
    
  if ($column_name == '_custom_id') {
    $custom_id  = get_post_meta( $post_id, '_custom_id', true );
      echo  $custom_id;
    }
}

By using of this code, our custom field will becomes a column and it’s value will be fill-up in the appropriate post value of every row.Now you can change the custom post type as per own requirement.

Step 2 : Implementing the sorting code.
This is the code which is used for sorting the posts. See here

add_filter( 'parse_query', 'hct_custom_post_sort' );
function hct_custom_post_sort($query) {

    if ( ! is_admin() )
        return $query;

    global $current_screen;
    if ( isset( $current_screen ) && 'wp_study' === $current_screen->post_type ) {
        $query->query_vars['orderby']  = 'meta_value';
        $query->query_vars['meta_key'] = '_custom_id';
        $query->query_vars['order']    = 'ASC';
    }
}

This code is used to sort the post listing by custom value . you need to just past this code in file- functions.php . Now open the admin dashboard and see the results.

Conclusion :
default-image
Hope so it will help for sorting the posts but still if you have any query then please share with me .

Tags:

Author information - Heycode Tech

Every world can ask question and my website registered user give the suggestion.

See all posts by Author

Connect to author
  • Hiya!

    Guys, need your help or perhaps advice. Question for
    individuals who play table games. What game would you recommend for a kid with a view to its development?

    Maany thanks.

About us

Heycodetech is a platform where you can ask any questions related to the programming like PHP,jQuery, MYSQL,HTML , CSS and moreHere is the Expert group of programming.