• 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

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter TOMTH

    (@tomth)

    Bump, anyone familiar with sorting algorhytms in WordPress?

    Use the Order Parameters of the WP_Query class. Should help you out there.

    Thread Starter TOMTH

    (@tomth)

    Wow Jerry, jack of all trades? πŸ˜›

    Thread Starter TOMTH

    (@tomth)

    The trouble is I think I have 3 queries. And arrays aren’t my forte.

    Thread Starter TOMTH

    (@tomth)

    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.

    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

    (@tomth)

    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?

    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

    (@tomth)

    Yes πŸ™‚ Aww crud πŸ˜›

    Thread Starter TOMTH

    (@tomth)

    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.