Title: Wp query by php variable
Last modified: August 21, 2016

---

# Wp query by php variable

 *  [MI](https://wordpress.org/support/users/mariukas/)
 * (@mariukas)
 * [12 years ago](https://wordpress.org/support/topic/wp-query-by-php-variable/)
 * Hi,
 * Each my post has a php variable.
    That variable changes depending on frontend
   user actions (it’s something like voting). I would like to get all post on index
   page with wp_query, but i want to sort them by that variable and show only 20
   highest items.
 * It is possible to get that php variable in query?
 * Thanks

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years ago](https://wordpress.org/support/topic/wp-query-by-php-variable/#post-4961205)
 * Please explain what you mean by ‘php variable’. Is it a Custom Field? If so, 
   you can use it in a query as shown here in the Codex: [http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters)
 *  Thread Starter [MI](https://wordpress.org/support/users/mariukas/)
 * (@mariukas)
 * [12 years ago](https://wordpress.org/support/topic/wp-query-by-php-variable/#post-4961217)
 * No, it’s not a custom field. It’s just php variable like:
    `$total = '5.5'`
 * $total is different on each single.php
    Also that number is dynamic sum from 
   different parameters (custom field values, frontend user data (user can vote 
   and change total variable in that way), some other calculations). I suppose that
   I should write it to db or custom field (and update it each time it is changed?),
   but the question is – can i get this php variable on index page to use it with
   wp query?
 * Thanks
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years ago](https://wordpress.org/support/topic/wp-query-by-php-variable/#post-4961218)
 * The easiest way to use it in a query would be to place it in a Custom Field.
 *  Thread Starter [MI](https://wordpress.org/support/users/mariukas/)
 * (@mariukas)
 * [12 years ago](https://wordpress.org/support/topic/wp-query-by-php-variable/#post-4961219)
 * Yeah. However any ideas how to get it as variable without using custom field 
   or db? Or where is no way to do it?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years ago](https://wordpress.org/support/topic/wp-query-by-php-variable/#post-4961220)
 * If it is always in the same format, you should be able to get it after the query,
   but may not be possible to use it in the query.

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

The topic ‘Wp query by php variable’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [12 years ago](https://wordpress.org/support/topic/wp-query-by-php-variable/#post-4961220)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
