[wp-hackers] Sorting by column value

Lionel POINTET lionel.pointet at globalis-ms.com
Mon Mar 26 16:48:04 UTC 2012


Hi everyone!

You've got an interesting filter provided by WordPress to add some
"sortable" columns once you added your custom columns.
For your example, you'll need to do something like this :
add_filter('manage_edit-press_articles_sortable_columns', 'your_function');

Your function just has to add a new value to an array with the 'slug' of
your new column as the key and the field you want to order on as the value.

Since you have to order with a meta value, you need to create a new way to
order on your custom posts in telling WordPress what to do when the
'orderby' GET param is filled with your value. You can do that in filtering
the 'request'.

This link is explaining pretty much the same :
http://scribu.net/wordpress/custom-sortable-columns.html

Hope that helps.

Regards,
Lionel

2012/3/26 Dmitry Tokmakov <dmitry at getmoxied.net>

> Hi everyone,
>
> Short description of my problem:
> I have a custom post type called "press_articles",
>
> With the code below I have created custom column, with the custom value.
>
> add_filter('manage_press_articles_posts_columns' ,
> 'add_press_articles_columns');
> add_action( 'manage_press_articles_posts_custom_column','custom_columns',
> 10, 2 );
>
> function custom_columns( $column, $post_id ) {
>  switch ( $column ) {
>  case 'featured_article':
>    $featured_flag = get_post_meta($post_id,'dt_featured_press_radio',
> true);
>   if ( is_string( $featured_flag ) ) {
>     echo $featured_flag;
>   } else {
>     echo 'Unable to get value';
>   }
>   break;
>  }
> }
>
> Everything is working fine,
> but sorting by column is not.
>
> Any ideas on how implement sorting by column value ?
> like when you go to your posts and click by header of date column to sort
> your posts by date.
>
> Thanks,
> Dmitry
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list