Sort Topics Column By Number of Likes (Favorites)
-
I would like to sort the topics by the number of likes column on the admin edit screen.
This is what ive got:
<?php // make columns sortable ------------------------------------------------- // define which columns to be sortable add_filter( 'manage_edit-topic_sortable_columns', 'topic_likes_table_sorting' ); function topic_likes_table_sorting( $columns ) { $columns['likes_count'] = 'likes_count'; return $columns; } // add sortable to meta key add_action( 'pre_get_posts', 'likes_orderby' ); function likes_orderby( $query ) { if ( ! is_admin() ){ return; } $orderby = $query->get( 'orderby'); if ( 'likes_count' == $orderby ) { $query->set('meta_key','bbpress_topic_like_button_like'); $query->set('orderby','meta_value_num'); } }
I dont think there is a meta_key for favorites stored in the topic post_meta? I’m setting the number in the column by using the following:
$topic_id = bbp_get_topic_id($id); $likes_count = bbp_get_topic_favoriters($topic_id);
I think the favorites are stored with the users metadata. So my best guess is that ill need to write a function that stores/updates the number of favorites inside the post meta. Then and only then can I sort the posts by that new metakey. Can anyone point me in the right direction with this?
Thank you!
-
not an easy option.
yes favorites are stored against a user.
This gives the difficulty that you either:
a) count favorites on then fly – ie on every page load you would cycle through each user and log the topics they have favorited and count them into an array – this will slow the site and it has to happen on every page load. you’d then store these into post meta.
b) create some code that will count these using cron every so often, and update post meta
c) link to the favorite button. So it would need to add when favorited and deduct when unfavorited. then you would need to allow for topics being trashed etc.a) is the easiest, but still a chunk of code and has site implications
b) is probably the most practical, but will only be as current as you run cron, so could be say an hour out of date
c) is a real bunch of code I suspect, but would be the best solution on a big website.
- You must be logged in to reply to this topic.