Title: using php variable in loop
Last modified: August 20, 2016

---

# using php variable in loop

 *  Resolved [eddioot](https://wordpress.org/support/users/eddioot/)
 * (@eddioot)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/using-php-variable-in-loop/)
 * Hi all,
 * on my website users can give the posts a +1 or -1 vote.
    The sum of all the votes
   is stored in a variable called $postscore in each post.
 * Is it possible to use this variable in the wordpress loop? My goal is to create
   a hotpage that displays the top rated posts from the last 2 weeks..
 * I googled a lot but did not find anything in this direction..

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/using-php-variable-in-loop/#post-2692343)
 * Is this value being stored in the database?
 *  Thread Starter [eddioot](https://wordpress.org/support/users/eddioot/)
 * (@eddioot)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/using-php-variable-in-loop/#post-2692347)
 * No there is a table in the database where i get the value from. It is the sum
   of all votes for the post id, my code is:
 *     ```
       <?php
       $post_id = $post->ID;
       $score = $wpdb->get_var( $wpdb->prepare(
                       "
                                      SELECT sum(rating_rating)
                                      FROM pp_ratings
                                      WHERE rating_postid =%s
                       ",
                       $post_id
       ) );
       $postscore = "{$score}";
   
                                                      ?>
       ```
   
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/using-php-variable-in-loop/#post-2692351)
 * Then you will have to run that query for every post in the Loop.
 *  Thread Starter [eddioot](https://wordpress.org/support/users/eddioot/)
 * (@eddioot)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/using-php-variable-in-loop/#post-2692356)
 * Yes i put the query in the loop so every post has his $postscore .
    I was just
   wondering if i am able to sort the posts to be displayed in descending order 
   for the $postscore.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/using-php-variable-in-loop/#post-2692368)
 * Not unless you change your approach & store this value in a custom field for 
   each post.
 *  Thread Starter [eddioot](https://wordpress.org/support/users/eddioot/)
 * (@eddioot)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/using-php-variable-in-loop/#post-2692370)
 * ok, thanks for the advise. I will look into that. 🙂
 *  Thread Starter [eddioot](https://wordpress.org/support/users/eddioot/)
 * (@eddioot)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/using-php-variable-in-loop/#post-2692706)
 * It was indeed fixed by using custom fields.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘using php variable in loop’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: [eddioot](https://wordpress.org/support/users/eddioot/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/using-php-variable-in-loop/#post-2692706)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
