Title: query_posts() help
Last modified: August 20, 2016

---

# query_posts() help

 *  [nvaughan84](https://wordpress.org/support/users/nvaughan84/)
 * (@nvaughan84)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/query_posts-help/)
 * Hi,
 * I have a query_post() that need to order first by meta data in descending order
   and then by random.
 * I have: query_posts($GLOBALS[‘query_string’].’&orderby=packageID rand&order=DESC’)
 * This works in that is first orders by packageID and then randomly orders within
   the results. The only problem is the order=DESC doesn’t work.
 * Is there a way to get is to order by the first column in DESCENDING order and
   then radomise the results?
 * If not, can the generated SQL from the query_posts() be output so I can create
   my own query?
 * Thanks

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

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/query_posts-help/#post-2918379)
 * is “packageID” a [custom field](http://codex.wordpress.org/Custom_Fields) and
   a number?
    Try this [untested]:
 *     ```
       query_posts($GLOBALS['query_string'].'&orderby=meta_value_num rand&meta_key=packageID&order=DESC');
       ```
   
 * [http://codex.wordpress.org/Function_Reference/WP_Query#Order_.26_Orderby_Parameters](http://codex.wordpress.org/Function_Reference/WP_Query#Order_.26_Orderby_Parameters)
 * Not sure I understand how you want it to be ordered by package ID and randomly
   though.
 *  Thread Starter [nvaughan84](https://wordpress.org/support/users/nvaughan84/)
 * (@nvaughan84)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/query_posts-help/#post-2918392)
 * Hi there,
 * thanks for the suggestion but no luck unfortunately.
 * Yep, packageID is a meta_key with a value of 1,2 or 3.
 * I need the posts to display with packageID’s 3 first, followed by 2 and finally
   1. Within these groups of 3,2 and 1 I need the posts to be random.
 * It nearly works but is showing them in the wrong order (packageID 1, 2 and then
   3) and the order=DESC doesn’t have any affect
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/query_posts-help/#post-2918400)
 * So packageID posts can have the same meta value. I don’t think this can be done
   with a (one) query. Do you need pagination for this?
 *  Thread Starter [nvaughan84](https://wordpress.org/support/users/nvaughan84/)
 * (@nvaughan84)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/query_posts-help/#post-2918402)
 * There can be a few posts with the same packageID (so 4 posts with packageID =
   1, 6 posts with packageID = 2 and 8 posts with packageID = 3)
 * No, I don’t think pagination is necessary.
 * Is there a way to get the SQL statement that the query_posts() function generates?
   I could then use this and amend it to create my own
 * Thanks for your help 🙂
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/query_posts-help/#post-2918420)
 * Try it with this: [http://pastebin.com/ihmi86jN](http://pastebin.com/ihmi86jN)
 * It will first get all distinct meta values for the packageID custom field and
   and orders them descending. Then it gets all posts for that meta value ordered
   randomly. Hope this helps.

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

The topic ‘query_posts() help’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [13 years, 10 months ago](https://wordpress.org/support/topic/query_posts-help/#post-2918420)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
