1

I would like to query by two different post types (exhibitions and posts) and output them (merged) by their two different date fields.

  • "Exhibitions" uses an ACF custom field for the date (exhibition_date_start).
  • "Posts" uses Wordpress default date.

The output should only show items dated before current date

Current output I get is not merged, but after each other, like this:

  • 170811 – Exhibition
  • 170809 – Exhibition
  • 170802 – Exhibition
  • 170810 – Post
  • 170807 – Post
  • 170801 – Post

What I want is:

  • 170811 – Exhibition
  • 170810 – Post
  • 170809 – Exhibition
  • 170807 – Post
  • 170802 – Exhibition
  • 170801 – Post

Is this doable with wp_query?

  • querying for posts with multiple types is trivial, but the sorting aspect of your question is non-trivial, and will probably require an expensive complex SQL query. Is there a particular reason an ACF field was used instead of the standard post date? Queries can be modified to show dates in the future if the visibility was the reason for this – Tom J Nowell Aug 24 '17 at 15:14
  • Take a look here stackoverflow.com/questions/37090575/… You can also add the same meta_value for post on 'save_post' action and then filter both in WP_Query – Anton Lukin Aug 24 '17 at 15:24
  • you can do this. You would could to do a wp_query for each data set, in the loop, in the query add posts to an array, then outside the wp_queries merge the 2 arrays.There's a bit of a complexity because you'll have to change one field or the other to the same name in one of the arrays. Lastly use usort based on the datefield. In your new combined array, you could then do a foreach to pull the data back out. – rudtek Aug 24 '17 at 16:02
0

I solved it with a bit of JS instead. Queried the two post types in a merged query and outputted each item with a 'data-sort' tag on each element. Then sorting the elements independently.

$archive_items.sort(function(a,b) {
    var an = a.getAttribute('data-sort'),
        bn = b.getAttribute('data-sort');                   
    if(an < bn) {
        return 1;
    }
    if(an > bn) {
        return -1;
    }
    return 0;
});
$archive_items.detach().appendTo($archive);

Perhaps not the most elegant and clean solution.

But thanks to Tom, Anton and rudtek for confirming this was not a standard and easy query.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.