0

Function to return the meta value as per author.

 <?php 

function ac_author_score() {

    $userID = get_the_author_meta( 'ID' );
    $query = array (
        'author' => $userID,
        'posts_per_page' => -1
    );
    $queryObject = new WP_Query($query);

    while( $queryObject->have_posts() ) : $queryObject->the_post();

    global $post;
    $postID = $post->ID;

    $score = $score + intval( get_post_meta( 'ratings_score', $postID, true ) );

    endwhile; 

    wp_reset_query();

    if ( $score > 0 )
        $score = '+' . $score;

    return $score;


}
?>

So it's returning the $score, but I can't get it to output anything with echo etc.

How do I actually print/echo the returned value?

4
  • The code should be correct. It may be the logic of your function that does not work. Try setting $score = 0; at the beginning and see if you can print that.
    – fischi
    Commented Feb 5, 2014 at 12:22
  • I can't seem to print anything, if I set $score = 10; then do echo or print_r after the return $score, nothing gets output.
    – andy
    Commented Feb 5, 2014 at 12:37
  • 2
    You do it like echo ac_author_score();, right?!
    – fischi
    Commented Feb 5, 2014 at 12:39
  • Yup, it turns out that it was just the code that was wrong, I'll post an answer with the correct code to display the score for anyone else who happens across this post.
    – andy
    Commented Feb 5, 2014 at 14:24

1 Answer 1

0

Correct way to get the ratings for individual authors.

This is for the author.php page. If you want to display it elsewhere on your site, for example to show the number of likes for the currently logged in user, then use get_current_user_id( 'ID' ); in place of get_the_author_meta.

 <?php 

    function author_rating_total() {
       $user_id = get_the_author_meta( 'ID' );
       $query = array (
           'author' => $user_id,
           'suppress_filters' => 'true',
           'posts_per_page' => -1
       );
       $queryObject = new WP_Query($query); while($queryObject->have_posts()) : $queryObject->the_post();

       $post_ratings_data = get_post_custom(get_the_id());
       $post_ratings_score = intval($post_ratings_data['ratings_score'][0]);

       $ratings_array[] = $post_ratings_score;

       endwhile; 

       $ratings_sum = array_sum($ratings_array);

       if ($ratings_sum > 0) {
             $ratings_sum = '' . $ratings_sum;
       }

       echo $ratings_sum;   

       wp_reset_query();
    }
    ?>

    <?php 

    echo author_rating_total();


    ?>

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.