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.

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!

share|improve this question
    
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? –  Johannes Pille Nov 24 '11 at 23:32
    
i'm not using a plugin, every post haves a "votes" meta key with a numbered value. thanks! –  Philip Nov 25 '11 at 6:40
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
thanks a lot @Johannes Pille, it's working as i need!!! –  Philip Nov 25 '11 at 8:11
    
it returns the total number of the latest post by user –  Philip Dec 2 '11 at 19:47
    
i leave it open to get some answers! thanks a lot! –  Philip Dec 2 '11 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. –  Johannes Pille Dec 2 '11 at 19:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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