Display number of favourites for listing

This topic contains 10 replies, has 3 voices, and was last updated by  Kiran 5 years, 3 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket

Tagged: 

  • Author
    Posts
  • #505736

    evocativemedia
    Expired Member
    Post count: 28

    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?

    #505743

    Alex Rollin
    Moderator
    Post count: 27815

    No, but let me ask the developers and get back to you about that.

    #505750

    evocativemedia
    Expired Member
    Post count: 28

    Oke, 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…

    #505796

    Alex Rollin
    Moderator
    Post count: 27815

    No, 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.

    #509541

    evocativemedia
    Expired Member
    Post count: 28

    Thanks 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

    #509905

    Kiran
    Moderator
    Post count: 7069

    Hi,

    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,
    Kiran

    #510049

    evocativemedia
    Expired Member
    Post count: 28

    Wow 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

    #510060

    evocativemedia
    Expired Member
    Post count: 28

    Minor 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.

    #510106

    Kiran
    Moderator
    Post count: 7069

    Hello,

    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

    #510306

    evocativemedia
    Expired Member
    Post count: 28

    Kiran 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.

    #510309

    Kiran
    Moderator
    Post count: 7069
    This reply has been marked as private.
Viewing 11 posts - 1 through 11 (of 11 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount