0

I would like to build one big query that is sorted by 2 custom fields (type date). I have a query that needs to have 2 post types ['track_trip', 'training']. Those 2 types have ACF custom fields that stand for future dates. ['date_track', 'date_training']. How should I combine my query to sort those types based on a custom field? Both are date type of course.

new WP_Query([
 'post_type' => ['tourist_trip', 'training'], 
 'post_status' => 'publish', 
 'meta_key' => 'date_tourist',  ??
 'orderby' => 'meta_value', 
 'order'=> 'ASC'];
])

Thanks guys for tips. I was able to solve it.

$args_track = [
      'post_type' => ['track_trip', 'training'],
      'post_status' => 'publish',
      'meta_query' => array(
                       'relation' => 'OR',
                      'date_track_sort' => array(
                       'key' => 'date_track',
                       'type' => 'DATE'
                      ),
                       'date_training_sort' => array(
                         'key' => 'date_training',
                         'type' => 'DATE'
                       ),
      )                     ,
      'orderby' => ['date_track_sort' => 'ASC', 'date_training_sort' => 'ASC']];
    ```

1
  • 1
    have you tried to use the meta_query parameter yet? There are a lot of parameters and examples documented at the official WP dev docs for WP_Query at developer.wordpress.org/reference/classes/wp_query/… including how to query meta fields and how to sort things. Your meta values will need to be in a particular format though, and if they aren't in that format you will need to contact ACF support or ask other ACF users how to fix that for your fields ( ACF/3rd party plugin support is offtopic here )
    – Tom J Nowell
    Commented Dec 9, 2022 at 18:03

1 Answer 1

0

I'm not clear on whether you're looking for one big query that returns all the 'track_trip' and 'training' posts, sorted by the two different custom meta fields, or two separate queries. But either way, you'll need to use a custom meta_query. You will probably also need to orderby meta_value_datetime. Note in the "‘orderby’ with multiple ‘meta_key’s" section of the WP_Query documentation you can orderby an array of values, if that's what you're looking to do.

2
  • I would like to return one big query.
    – Masmiix
    Commented Dec 9, 2022 at 17:28
  • @Masmiix can you update your original question with that?
    – Tom J Nowell
    Commented Dec 9, 2022 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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