Title: Custom Index Query &#8211; Sorting issue
Last modified: August 20, 2016

---

# Custom Index Query – Sorting issue

 *  [TOMTH](https://wordpress.org/support/users/tomth/)
 * (@tomth)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/)
 * Hello,
 * I have a custom query to display certain items on my homepage. I have three categories:
   Random(1), Results(4), Cup(6). What it does now is first get the newest post 
   in category 4, the newest post in category 6, then all posts in category 1.
 * What I would like to add, is to sort all the posts by date. So cat 4 and 6 still
   only display 1 post each, it will be sorted by date.
 * All help is much appreciated.
 * Code: [http://pastebin.com/R70TrVAv](http://pastebin.com/R70TrVAv)

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

 *  Thread Starter [TOMTH](https://wordpress.org/support/users/tomth/)
 * (@tomth)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480659)
 * Bump, anyone familiar with sorting algorhytms in WordPress?
 *  [Jay](https://wordpress.org/support/users/phyrax/)
 * (@phyrax)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480666)
 * Use the [Order Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters)
   of the WP_Query class. Should help you out there.
 *  Thread Starter [TOMTH](https://wordpress.org/support/users/tomth/)
 * (@tomth)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480686)
 * Wow Jerry, jack of all trades? 😛
 *  Thread Starter [TOMTH](https://wordpress.org/support/users/tomth/)
 * (@tomth)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480687)
 * The trouble is I think I have 3 queries. And arrays aren’t my forte.
 *  Thread Starter [TOMTH](https://wordpress.org/support/users/tomth/)
 * (@tomth)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480691)
 * So this is what it does now.
 * Loop through cat 6, get newest post and post it.
    Loop through cat 4, get newest
   post and post it. Post all other posts (newest first).
 * First post is always category 6 and second post always category 4, even though
   a post in category 1 is newer than one in cat 4 or 6.
 *  [Jay](https://wordpress.org/support/users/phyrax/)
 * (@phyrax)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480693)
 * Okay so query one and two (cat 6 & 4 respectively) are fine, but query 3, should
   just exclude cats 6 & 4.
 * Your third query instead of reading ‘cat=1’ can read ‘cat=-6,-4’ therefore excluding
   said category ids.
 *  Thread Starter [TOMTH](https://wordpress.org/support/users/tomth/)
 * (@tomth)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480697)
 * That doesn’t change anything. Instead of sorting ALL the posts by date, it will
   first show result from query 1, result from query 2, result from query 3. Being
   1 post from cat 6, 1 post from cat 4, and the rest. This is fine, except I can’t
   get it all to be sorted by date.
 * I’m not sure if this is clearer?
 *  [Jay](https://wordpress.org/support/users/phyrax/)
 * (@phyrax)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480699)
 * Okay, so you want to query all categories, (1, 4, & 6) but you want only one 
   post from cats 4 & 6, and you still want them to be in chronological order.
 * EDIT: I don’t know if that’s possible haha
 *  Thread Starter [TOMTH](https://wordpress.org/support/users/tomth/)
 * (@tomth)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480700)
 * Yes 🙂 Aww crud 😛
 *  Thread Starter [TOMTH](https://wordpress.org/support/users/tomth/)
 * (@tomth)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480703)
 * Solved it with a workaround, but would be better if I could get it automated.
   Created an extra two categories, which will be manually controlled to only have
   1 post in it. Each time a new post is created, the category needs to be edited.

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

The topic ‘Custom Index Query – Sorting issue’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 10 replies
 * 2 participants
 * Last reply from: [TOMTH](https://wordpress.org/support/users/tomth/)
 * Last activity: [14 years, 4 months ago](https://wordpress.org/support/topic/custom-index-query-sorting-issue/#post-2480703)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
