Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Im using the buddypress likes plugin and I'm using a code to show the like count for posts on the index page of my site. The problem Im running into is the Like count only shows if you've liked the post and then it shows the total number. If you're logged out you won't see any like counts. How can I show the Like Counts to everyone? Here's how I call it inside the index page loop.

<?php if ( bp_like_is_liked( $id, 'blogpost' ) ) : ?>
    <?php 
      $liked_count = count( get_post_meta( $id, 'liked_count', true ) );
      if ( $liked_count ) echo 'Likes '. $liked_count . ''; ?>
    <?php endif; ?>

I changed it so now all the liked show but for some reason all posts show atleast a like count of 1 even when they don't have any. Any idea why that is. Here's what I changed.

<?php 
      $liked_count = count( get_post_meta( $id, 'liked_count', true ) );
      if ( !$liked_count==0 ) echo 'Likes '. $liked_count . ''; ?>
share|improve this question

closed as off-topic by Pieter Goosen, kaiser Aug 4 at 16:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
I changed if ( !$liked_count==0 ) to if ( !$liked_count > 0 ) which didn't change anything. They I set it to > 1 and then it made all the counts disappear that were actually 1 and others that were set 0 and showing 1. So nothing solved yet. –  Pollux Khafra Mar 24 '12 at 1:39

1 Answer 1

up vote 0 down vote accepted

You are using the count() function to determine $liked_count. This function is meant to determine the number of items in an array. But it appears that the value stored in the liked_count postmeta is not an array, but an integer count. When you do count( $p ) where $p is an integer, you get 1 no matter what the integer is.

Try this instead:

$liked_count = (int) get_post_meta( $id, 'liked_count', true );
share|improve this answer
    
That oddly enough returns a 1 for any post that has been liked at least once. –  Pollux Khafra Mar 25 '12 at 21:08
    
What does it look like when you var_dump( get_post_meta( $id, 'liked_count', true ) ); ? –  Boone Gorges Mar 25 '12 at 22:03
    
This is what it shows for a post with two likes array(2) { [1]=> string(10) "user_likes" [4]=> string(10) "user_likes" } Likes ...this is what it shows for a post with 1 like array(1) { [1]=> string(10) "user_likes" } Likes ...and this is 0 liked string(0) "" Likes –  Pollux Khafra Mar 25 '12 at 22:23
    
Oh. Then count() should work. –  Boone Gorges Mar 26 '12 at 10:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.