Created
September 2, 2015 19:11
-
-
Save kylephillips/dd81584e30b35a74d632 to your computer and use it in GitHub Desktop.
Add favorite counts to the WP admin post tables
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 | |
// Place this code in your theme's functions.php file | |
/** | |
* First, add the column to the view | |
* Change the post type by substituting 'post' with the post type | |
* Ex: a post type of recipe would be manage_recipe_posts_columns | |
*/ | |
add_filter('manage_post_posts_columns', 'add_favorite_count_column'); | |
function add_favorite_count_column($columns) | |
{ | |
$new_column = array('favorites' => __('Favorites')); | |
return array_merge($columns, $new_column); | |
} | |
/** | |
* Next, add the column data | |
*/ | |
add_action('manage_posts_custom_column', 'add_favorites_data_to_column', 10, 2); | |
function add_favorites_data_to_column($column, $post_id) | |
{ | |
if ( $column !== 'favorites' ) return; | |
echo get_favorites_count($post_id); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This sorta works for getting a sortable column... If a post has not been favorited then it won't appear in the sorted view