0

In author.php, i have the list with the posts written by the author.
I'm trying to get the total number of votes, from the author posts.
All posts have a post_meta with a numbered value. i need to count the total votes from all the posts that made from this user/author.

Is that possible? is a query available, that can handle this request?

thanks a lot!

2
  • The database table wp_postmeta has 4 columns - under what meta_key are the votes saved? If you don't know that - what plugin are you using? Commented Nov 24, 2011 at 23:32
  • i'm not using a plugin, every post haves a "votes" meta key with a numbered value. thanks!
    – Philip
    Commented Nov 25, 2011 at 6:40

1 Answer 1

1

Assuming that you have a loop within your author.php file, grab the required metadata for every post within it, add it into a variable and display that after the loop has finished:

$author_vote_count = 0; //declare vote count variable before the loop

<?php while ( have_posts() ) : the_post(); ?>

    // the stuff going on in your loop already

    <?php if ( get_post_meta($post->ID, 'votes', true) ) {
        $author_vote_count = $author_vote_count + get_post_meta($post->ID, 'votes', true);
    } ?>

<?php endwhile; ?>

<?php echo "Total votes on author's posts: " . $author_vote_count; ?>

The above will only work as expected, if the meta_value of the key votes is an integer (or any type of number). Should it be saved in the database as a string, you need to convert it to an integer first. In that case use

$author_vote_count = $author_vote_count + intval( get_post_meta($post->ID, 'votes', true) );

instead.

3
  • it returns the total number of the latest post by user
    – Philip
    Commented Dec 2, 2011 at 19:47
  • i leave it open to get some answers! thanks a lot!
    – Philip
    Commented Dec 2, 2011 at 19:48
  • @Philip Have you by any chance put the $author_vote_count = 0 inside the while loop? Or are you trying to use the above on single post pages? If neither is the case, this will work as it should. If your author.php lists all post by an author and you've put the above snippets in their respective places they will return all votes on all posts by that author. Commented Dec 2, 2011 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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