0

I recently posted a similar question, but it didn't seem anyone could help - I've thought of a simpler method below if anyone can offer any advice? Been trying for 3 days on this with no luck, am sure it's something fairly simple but it's a bit beyond my skill set I'm afraid!

Essentially I'd like to add some code to functions.php so that for each category/tag/custom taxonomy archive:

From the current query, get the entire list of posts and check to see if any posts are in cat ID 283 (expired)

if not "Expired", order all posts by meta value "savings" DESC

Then list the remaining "Expired" posts beneath the above list (in any order - although if possible by post ID DESC)

So essentially

  1. Get entire post ID list for current taxonomy.
  2. Get all non expired posts and order by meta value "savings" DESC
  3. Get all expired posts and order by post ID DESC
  4. Combine these into one list so they appear Not Expired then Expired
  5. Apply to current taxonomy so that it works with pagination.

I've tried loads of different pieces of code from here over the last few days, and I can either get it to:

  1. Order all posts by the meta key, or

  2. Order all posts non expired, then expired (although this has a few issues with pagination)

However I haven't managed to combine the two.

There's a couple of half solutions here: Display all posts in category, with specific tag posts at top

which I've modified so non expired posts are listed above expired ones, but I didn't manage to further combine it with ordering by meta key.

I've also tried https://github.com/birgire/wp-combine-queries without much luck.

If anyone has any pointers I'd be very grateful.

Thanks so much in advance

Joey

  • another way of doing this sort it to create a new meta like specialOrder. if the post is expired, you store the post identifier (and an offset) and when the post is not expired, you store the value of "savings". finally yo just have to sort all post on the meta specialOrder. – mmm Jun 27 '18 at 18:31
  • Thanks mmm - I do already have a plugin which automatically calculates the meta key value, so I might be able to modify that to automatically add 0 to the "savings" key for any posts which are in "expired". – Joey Jun 28 '18 at 6:54
  • My solution was to set all expired posts' meta key "savings" to "0", and then just order posts by this meta key DESC. – Joey Jun 28 '18 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.