Skip to:
Content
Pages
Categories
Search
Top
Bottom

Get Number of Favorites & Subscription to a topic

  • @babblebey

    Participant

    I am trying to make up my own topic description/summary area, so I would like to display the current number of favourites and subscriptions the current topic has.

    I have done successfully some other part of the area

    
    <?php !bbp_show_lead_topic() ? bbp_topic_reply_count() : bbp_topic_post_count(); // Display No. of replies in current topic ?>
    <?php bbp_topic_voice_count(); //Display No of Users in current topic ?>
    <?php bbp_author_link( array( 'post_id' => bbp_get_topic_last_active_id(), 'type' => 'avatar', 'size' => 20 ) ); //Display Last Reply User ?>
    <?php bbp_topic_freshness_link(); //Display Last Reply Time ?>
    

    So I currently want a function that will display the number of favourites(priority) and subscriptions(not much priority).

    Thanking you in anticipation of any working response.

Viewing 23 replies - 1 through 23 (of 23 total)
  • @robin-w

    Moderator

    under 2.5.14 favorites are stored in usermeta under ‘wp__bbp_favorites’ as a list of topics

    so to get a count for a topic, you would need to loop through all users and for each one count if the current topic was in it.

    so something like (not tested as am tied up elsewhere, so just spending 5 mins on this)

    $topic = some function to get current topic number if not already there
    $users = get_users() ;
    $count = 0 ;
    foreach ($users as $user) {
    	$topic_list = get_usermeta ($user->id, 'wp__bbp_favorites', false) ;
    	if (in_array ($topic , $topic_list) $count++ ;
    }
    echo $count ;

    @babblebey

    Participant

    I guess I will be patient for you to test this Robin

    
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $count = 0;
    foreach ($users as $user) {
        $topic_list = get_usermeta ($user->id, 'wp__bbp_favorites', false) ;
        if (in_array ($topic , $topic_list))  $count++ ;
    }
    echo $count;
    

    I am getting the error

    Warning: in_array() expects parameter 2 to be array, string given in C:\wamp\www\omna\wp-content\themes\omna2\bbpress\content-single-topic-lead.php on line 134

    @babblebey

    Participant
    
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $count = 0;
    foreach ($users as $user) {
        $topic_list = array(get_usermeta ($user->id, 'wp__bbp_favorites', false)) ;
        if (in_array ($topic , $topic_list))  $count++ ;
    }
    echo $count;
    

    And this one worked Robin! Thank you!

    @robin-w

    Moderator

    great – I think subscriptions works the same way but with ‘wp__bbp_subscriptions’

    @babblebey

    Participant
    
    <?php
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $countsub = 0;
    foreach ($users as $user) {
        $topicsub_list = array(get_usermeta ($user->id, 'wp__bbp_subscriptions', false)) ;
        if (in_array ($topic , $topicsub_list))  $countsub++ ;
    }
    echo $countsub;
    ?>
    

    It didn’t work that way, Robin!

    I got a 0 in the result, even as i was sure there’s a subscription

    @robin-w

    Moderator

    hmm, can’t see why it shoudln’t work

    try for test purposes

    <?php
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $countsub = 0;
    foreach ($users as $user) {
        $topicsub_list = array(get_usermeta ($user->id, 'wp__bbp_subscriptions', false)) ;
    echo '<br>Topic : '.$topic ;
    echo '<br>user_id: '.$user->id.'<br>' ;
    var_dump ($topicsub_list) ;
        if (in_array ($topic , $topicsub_list))  $countsub++ ;
    }
    echo $countsub;
    ?>

    that should help you find out where the problem is

    @babblebey

    Participant

    Ok, so I’ve got this result, Robin!

    
    Topic : 65
    user_id: 1
    array (size=1)
      0 => string '149,65' (length=6)
    
    Topic : 65
    user_id: 2
    array (size=1)
      0 => string '145,146,65' (length=10)
    
    0
    

    I believe that should mean that User 1 and User 2 and currently subscribed to the topic But I still get a 0. The Replica of the Favorite should actually work but I don’t know..

    @robin-w

    Moderator

    looks like it hasn’t converted to an array basically it should read

    array (size = 2)
    0 => 149
    1 => 65

    etc.

    ok so let’s try replacing

    $topicsub_list = array(get_usermeta ($user->id, 'wp__bbp_subscriptions', false)) ;

    with

    $topicsub_lista = get_usermeta ($user->id, 'wp__bbp_subscriptions', true) ;
    $topicsub_list = explode (',' , $topicsub_lista) ; 

    @babblebey

    Participant

    That was a hit, Robin!

    
    <?php
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $countsub = 0;
    foreach ($users as $user) {
        $topicsub_listarray = get_usermeta ($user->id, 'wp__bbp_subscriptions', true) ;
        $topicsub_list = explode (',' , $topicsub_listarray) ;
        if (in_array ($topic , $topicsub_list))  $countsub++ ;
    }
    echo $countsub;
    ?>
    

    That worked it….. Thank you.

    @robin-w

    Moderator

    great – you may want to check the favorites for if a user has more than one favorite topic – I am suspecting your code was only tested with 1 favorite – if so same principal, if not I don’t know why one works and then other doesn’t !!

    @babblebey

    Participant

    My Code was to test it for how many User has favourited a particular topic (Like, check how many likes a post has)…

    shots

    But I guess like you said, If I understand you very well, I am testing in a scenario where User has only one topic listed in the wp__bbp_favorites meta area…

    I think that fact has returned to haunt me now Robin… The picture above shows that the Currently logged in User (Who is a keymaster) has favorited the current topic but, his Favorite doesn’t count, the displayed 1 is for that of the second user who also favorites the same topic and happens to be the only topic he has favorited.

    Look at the shot below then:

    screenshot-localhost-2019-11-07-17-12-45

    Now I have added more topics to the user’s wp__bbp_favorites by favouriting more topics but Its not displaying the favourites in those topics.

    Another scenario on a different topic here:

    screenshot-localhost-2019-11-07-17-22-30

    Not displaying the number of favorite for the topic.

    I added a new user and it was able to successfully favourite and increase the count on one of those topics that didn’t get from the previous two users.

    So I think the favorite count only work if a certain user has only one topic inside their wp__bbp_favorites

    Here’s the favourite count code again as I have used:

    
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $count = 0;
    foreach ($users as $user) {
        $topic_list = array(get_usermeta ($user->id, 'wp__bbp_favorites', false)) ;
        if (in_array ($topic , $topic_list))  $count++ ;
    }
    echo $count;
    

    What do you suggest I do now Robin? Should I try explode explosion???

    @babblebey

    Participant

    Now I guess if Each User has already one topic in his wp__bbp_favorites the topic stays favorited and counted but No counts is allowed for another topic again.

    So the if (in_array ($topic , $topic_list)) $count++ is being a Jealous line i.e: It Only counts a user’s favorite if the user favorites only the currents topics else it doesn’t. because there exists more than one topic inside the $topic_list = array(get_usermeta ($user->id, 'wp__bbp_favorites', false)) ;

    What do I do?

    @babblebey

    Participant
    
    <?php
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $countfav = 0;
    foreach ($users as $user) {
        $topicfav_listarray = get_usermeta ($user->id, 'wp__bbp_subscriptions', true) ;
        $topicfav_list = explode (',' , $topicfav_listarray) ;
        if (in_array ($topic , $topicfav_list))  $countsub++ ;
    }
    echo $countfav;
    ?>
    

    This worked now Robin! Thank you!

    @babblebey

    Participant
    
    <?php
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $countsub = 0;
    foreach ($users as $user) {
        $topicsub_list = array(get_usermeta ($user->id, 'wp__bbp_subscriptions', false)) ;
        if (in_array ($topic , $topicsub_list))  $countsub++ ;
    }
    echo $countsub;
    ?>
    

    Now it’s understandable that this also didn’t work on the topic I tested it with, because the user has subscribed to multiple topics.

    Oh wow!

    @babblebey

    Participant

    Counting Topic Favorites:

    
    <?php
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $countfav = 0;
    foreach ($users as $user) {
        $topicfav_listarray = get_usermeta ($user->id, 'wp__bbp_subscriptions', true) ;
        $topicfav_list = explode (',' , $topicfav_listarray) ;
        if (in_array ($topic , $topicfav_list))  $countfav++ ;
    }
    echo $countfav;
    ?>;
    

    Counting Topic Subscriptions:

    
    <?php
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $countsub = 0;
    foreach ($users as $user) {
        $topicsub_listarray = get_usermeta ($user->id, 'wp__bbp_subscriptions', true) ;
        $topicsub_list = explode (',' , $topicsub_listarray) ;
        if (in_array ($topic , $topicsub_list))  $countsub++ ;
    }
    echo $countsub;
    ?>;
    

    Now those are the things that worked.

    Thank you, Robin! I hope this helps someone in the future

    @robin-w

    Moderator

    great – and thanks for posting the final solutions – I’m sure it will help someone else

    @specstanza

    Participant

    Hi @babblebey & @robin-w , thank you both for your work on this matter.

    I’m trying to do the same on my Bbpress install (not online yet) in order to sort topic by number of “fav mentions”.

    I tried your last two codes but it returns “0” (I hooked them on bbp_template_before_single_topic and putted them in two separated functions).

    Does it still works for you? Did I get it all wrong?

    Thank you for your answer. 🙂

    @robin-w

    Moderator

    can you post the complete code

    @specstanza

    Participant

    Hi @robin-w – thanks for your answer.
    Here it is – as I’m still learning PHP, I apologize if I did a terrible coding mistake.
    I tried some modifications here and there (return, echoing other stuff) but nothing did the trick.

    /* tried to hook the code in my template */
    add_action('bbp_template_before_single_topic', 'show_bbp_sub');
    function show_bbp_sub() { 
    /* end of it */ 
     /* Original lines from @babblebey */
    $topic = bbp_get_topic_id();
    $users = get_users() ;
    $countsub = 0; /* I guess it's echoing only this line */
    foreach ($users as $user) {
        $topicsub_listarray = get_usermeta ($user->id, 'wp__bbp_subscriptions', true) ;
        $topicsub_list = explode (',' , $topicsub_listarray) ;
        if (in_array ($topic , $topicsub_list))  $countsub++ ;
    }
    echo $countsub;
    }
    

    Thank you for your help and your patience.

    @robin-w

    Moderator

    I’ve just taken a look – the whole way subscriptions works was changed in 2.6

    this code works on my test site

    add_action('bbp_template_before_single_topic', 'show_bbp_sub');
    function show_bbp_sub() { 
    	$subscriptions = bbp_get_subscribers(bbp_get_topic_id()) ;
    	$count = count($subscriptions) ;
    	echo 'number of users subscribed: '.$count;
    }

    @specstanza

    Participant

    Thank you so much @robin-w – changed the subscription to fav with bbp_get_topic_favoriters 🙂

    add_action('bbp_template_before_single_topic', 'show_bbp_sub');
    function show_bbp_sub() { 
    	$subscriptions = bbp_get_topic_favoriters(bbp_get_topic_id()) ;
    	$count = count($subscriptions) ;
    	echo 'number of users subscribed: '.$count;
    }

    Now I only have to look for a way to display my topics and sorted them by numbers of favs! Thanks to you I know there was some major changes with the 2.6 version.

    @robin-w

    Moderator

    🙂 great, glad to have helped

    @specstanza

    Participant

    A little update on this too. 🙂

    The $count of favoriters is updated correctly on topic’s single page.
    But when the $count is called elsewhere, WordPress seems to take the first _bbp_favorite encounter.

    Looking at posts’ metavalues, it seems that every ‘favorite’ mention becomes an extra _bbp_favorite

    bbp_favorite

    My code doesn’t say anything about taking the highest number – maybe this would be the solution?

    $subscriptions = get_post_meta(get_the_ID(), '_bbp_favorite', true) ;
    $count = count($subscriptions) ;
    $output .= '<li><a href="' . get_permalink() . '">' . get_post_meta(get_the_ID(), 'an_extra_field', true) . '</a><br> <span> Votes :'  . get_post_meta(get_the_ID(), '_bbp_favorite', true) . '</span></li>';
              }
              $output .= '</ul>';
              wp_reset_query();
         }
         return $output;
    }
Viewing 23 replies - 1 through 23 (of 23 total)
  • You must be logged in to reply to this topic.
Skip to toolbar