Custom Columns Orderby Problem (Wrong Order)
-
Hi, first up, i am new in writing my own stuff in WP or editing theme etc, so please don’t laugh π
I have a little problem. I added a custom post type, everything working fine. added custom columns at the admin panel. working fine too.
but i have a problem in making them sortable.
i added the following code in my functions.phpadd_filter("manage_edit-lmgevents_columns", "lmg_columns"); add_action("manage_lmgevents_posts_custom_column", "lmg_custom_columns"); function lmg_columns($columns){ $newcolumns = array( "cb" => "<input type=\"checkbox\" />", "title" => "Name", "startzeit-datum" => "Startzeit", "endezeit-datum" => "Endzeit", ); $columns= array_merge($newcolumns, $columns); return $columns; } function lmg_custom_columns($column){ global $post; switch ($column){ case "startzeit-datum": echo get_post_meta($post->ID,'startzeit-datum',true); break; case "endezeit-datum": echo get_post_meta($post->ID,'endezeit-datum',true); break; } } add_filter('manage_edit-lmgevents_sortable_columns', 'column_register_sortable'); function column_register_sortable($columns) { $columns['startzeit-datum'] = 'startzeit-datum'; $columns['endezeit-datum'] = 'endezeit-datum'; return $columns; }
i have added two columns, startzeit-datum and endezeit-datum. Thats the name/id (whatever) of my custom fields.
Now i can sort the columns by clicking on the column title, but i have no idea what WP is sorting. All my timestamps are just randomly ordered
http://www.testbetrieb.eu/problem1.png
a screenshot of the order WP is returning.in the url everything is looking fine i guess:
&orderby=startzeit-datum&order=desc.How can i fix my noobish code? Did i miss something?
-
ok, found my mistake.
addedadd_action( 'pre_get_posts', 'lmg_orderby' ); function lmg_orderby( $query ) { if( ! is_admin() ) return; $orderby = $query->get( 'orderby'); if( 'startzeit-datum' == $orderby ) { $query->set('meta_key','startzeit-datum'); $query->set('orderby','meta_value_num'); } if( 'endezeit-datum' == $orderby ) { $query->set('meta_key','endezeit-datum'); $query->set('orderby','meta_value_num'); } }
- The topic ‘Custom Columns Orderby Problem (Wrong Order)’ is closed to new replies.