I have two different post types. Trainings and dates. The post type for trainings has some basic informations about the specific training. Like a description, images and so on. The date ist for informations like the date (obviously), the trainer and location. And I add the training itself to the date.

I now want to sort all trainings by the next dates. As I understand, I need to loop through the dates first and after that I need to loop though the trainings based on the text dates.

I've found some examples with multiple meta_queries. But is it possible to loop though multiple post types?

  • It seems like instead of two custom post types you need a custom taxonomy + a custom post type. That would make the queries easier and faster. – WebElaine Nov 13 '17 at 16:03
  • yes, but it is necessary to have these post types. I couldn't do it with a custom taxonomy + a custom post type – Cray Nov 13 '17 at 16:04
  • If you need to sort and view in different ways, perhaps what you're looking for is having "date" as a taxonomy, then a post type of "training" and one for "event." Event would be what you're currently calling a date, so that "date" could actually be a numeric date assigned to both CPTs. From there, on the "training" archive you set the main query to order by the "date" taxonomy, and on the "events" archive you can do the same and then display all "training" CPTs within a single date if you like. – WebElaine Nov 13 '17 at 16:25
  • OK, but I've already a custom field with date input (with ACF). Isn't it possible to sort by that field? – Cray Nov 13 '17 at 16:45
  • Yes, but sorting by a custom field is a slow operation that puts a lot of load on your database. Taxonomy will keep your site running faster. – WebElaine Nov 13 '17 at 16:50

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.