Display number of favourites for listing
This topic contains 10 replies, has 3 voices, and was last updated by Kiran 5 years, 2 months ago.
We have moved to a support ticketing system and our forums are now closed.
Open Support TicketTagged: snippet
-
AuthorPosts
-
August 30, 2019 at 4:33 pm #505736
On each listings’ details page I have included a ‘favourite button’.
Using the following filters I can adjust the favourite text/contents:
add_filter('geodir_unfavourite_text', 'geodir_unfavourite_text_callback' ); function geodir_unfavourite_text_callback(){ return 'Favoriets <span>#favourites</span>'; } add_filter('geodir_favourite_text', 'geodir_favourite_text_callback' ); function geodir_favourite_text_callback(){ return 'Favoriets <span># favourites</span>'; }
This can be found in the file ‘geodirectory/includes/post-functions.php’
I wish to replace the ‘#’ in the above callbacks with the actual number of favourites for that listing. Is there a function that returns the number of favourites?
August 30, 2019 at 5:01 pm #505743No, but let me ask the developers and get back to you about that.
August 30, 2019 at 5:32 pm #505750Oke, if there is not one: I guess I would have to check for each listing against all members whether it is added as favourite by them?
Wouldn’t this be a nice feature? Opens up possibilities for lists of popular listings etc…
August 30, 2019 at 11:44 pm #505796No, there isn’t a feature available now. Maintaining the counts in the database would be the consideration. Right now there are several different types of counts being maintained. I have added your idea to our suggestion box and the developers will consider it for future versions.
September 21, 2019 at 3:27 pm #509541Thanks Alex for passing it on, fingers crossed for a updated future version. It would be really nice to actually use the favorite counts for popular/trending listings.
Kind regards
September 24, 2019 at 6:26 am #509905Hi,
Try following PHP snippet.
function gd_snippet_get_post_favs_count( $post_id ) { global $wpdb; $count = 0; if ( empty( $post_id ) ) { return $count; } $meta_key = 'gd_user_favourite_post'; $meta_value = 'i:' . (int) $post_id . ';'; if ( is_multisite() ) { $blog_id = (int) get_current_blog_id(); if ( $blog_id && $blog_id != 1 ) { $meta_key = '_' . $blog_id; } } $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value LIKE %s", array( $meta_key, '%' . $wpdb->esc_like( $meta_value ) . '%' ) ) ); return $count; } /** * Favorite */ function geodir_favourite_text_callback( $label ) { global $gd_post; $post_id = wp_doing_ajax() && ! empty( $_REQUEST['pid'] ) ? absint( $_REQUEST['pid'] ) : ( ! empty( $gd_post ) ? $gd_post->ID : NULL ); if ( ! empty( $post_id ) ) { $count = gd_snippet_get_post_favs_count( $post_id ); $label = '<span>' . $count . '</span> Favorites'; } return $label; } add_filter( 'geodir_favourite_text', 'geodir_favourite_text_callback', 20, 1 ); /** * Unfavorite */ function geodir_unfavourite_text_callback( $label ) { global $gd_post; $post_id = wp_doing_ajax() && ! empty( $_REQUEST['pid'] ) ? absint( $_REQUEST['pid'] ) : ( ! empty( $gd_post ) ? $gd_post->ID : NULL ); if ( ! empty( $post_id ) ) { $count = gd_snippet_get_post_favs_count( $post_id ); $label = '<span>' . $count . '</span> Favorites'; } return $label; } add_filter( 'geodir_unfavourite_text', 'geodir_unfavourite_text_callback', 20, 1 );
Regards,
KiranSeptember 24, 2019 at 6:19 pm #510049Wow Kiran thanks for this!
I had to change {$wpdb->usermeta} to wp_usermeta in the SQL statement to make it work, so from:
"SELECT COUNT(*) FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value LIKE %s"
to:
"SELECT COUNT(*) FROM wp_usermeta WHERE meta_key = %s AND meta_value LIKE %s"
Thank you so much!
Kind regards
September 24, 2019 at 6:39 pm #510060Minor UX notice: when the callback executes, database counts are not updated yet so although the favourite button invokes an ajax-request, the count in the favourite text is not properly updated.
September 25, 2019 at 6:22 am #510106Hello,
I have added some changes to update button text on favorite/unfavorite action.
Apply patch on your site from here https://github.com/AyeCode/geodirectory/commit/0cc94e6c4df78c183dd83efc6fbdb357caa10c9a and update PHP snippet from here https://wpgeodirectory.com/support/topic/display-number-of-favourites-for-listing/#post-509905
Kiran
September 26, 2019 at 6:11 am #510306Kiran you are amazing, this works (of course)! Very nice to see that you implemented this for future version. I’ll keep an eye out on the repo for more interesting patches..
Minor UX change I made:
if($count == 1) { $label = '<span>' . $count . '</span> Favorite'; } else { $label = '<span>' . $count . '</span> Favorites'; }
Thank you so much for implementing this so swiftly.
September 26, 2019 at 6:26 am #510309This reply has been marked as private. -
AuthorPosts
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket