Quantcast
Channel: Question and Answer » post-meta
Viewing all articles
Browse latest Browse all 54

How to add together (get the sum) a field from all author's posts

$
0
0

Using the plugin WPeddit I want to count the epicredvote score from all posts of an author together to get the sum.

The following is a custom field that puts out a score, like “3″ or “-2″ or whatever:

echo get_post_meta(get_the_ID(), 'epicredvote', true );

So I know, I have to build a loop somehow…

This is what I’ve found, but don’t get a number on $allscore:

$authorPost = get_posts( array( 'author' => 1 ) );

foreach( $catPost as $post ); {
    setup_postdata( $post );
    $ids[] = get_the_ID();
}

$idList     = implode( ",", $ids );     //turn the array into a comma delimited list
$meta_key   = 'epicredvote';
$allscore   = $wpdb->get_var( $wpdb->prepare("
        SELECT sum(meta_value) 
        FROM $wpdb->postmeta 
        WHERE meta_key = %s 
        AND post_id in (" . $idList . ")
    ",
    $meta_key
) );

echo 'user 1 All posts View Count' . $allscore . ' ';

Viewing all articles
Browse latest Browse all 54

Trending Articles