-
-
Save simonwhatley/3912718 to your computer and use it in GitHub Desktop.
'price' sortable column example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Register the column | |
function price_column_register( $columns ) { | |
$columns['price'] = __( 'Price', 'my-plugin' ); | |
return $columns; | |
} | |
add_filter( 'manage_edit-post_columns', 'price_column_register' ); | |
// Display the column content | |
function price_column_display( $column_name, $post_id ) { | |
if ( 'price' != $column_name ) | |
return; | |
$price = get_post_meta($post_id, 'price', true); | |
if ( !$price ) | |
$price = '<em>' . __( 'undefined', 'my-plugin' ) . '</em>'; | |
echo $price; | |
} | |
add_action( 'manage_posts_custom_column', 'price_column_display', 10, 2 ); | |
// Register the column as sortable | |
function price_column_register_sortable( $columns ) { | |
$columns['price'] = 'price'; | |
return $columns; | |
} | |
add_filter( 'manage_edit-post_sortable_columns', 'price_column_register_sortable' ); | |
function price_column_orderby( $vars ) { | |
if ( isset( $vars['orderby'] ) && 'price' == $vars['orderby'] ) { | |
$vars = array_merge( $vars, array( | |
'meta_key' => 'price', | |
'orderby' => 'meta_value_num' | |
) ); | |
} | |
return $vars; | |
} | |
add_filter( 'request', 'price_column_orderby' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment