Support

Account

Home Forums General Issues Query posts through relationships' taxonomies Reply To: Query posts through relationships' taxonomies

  • You are right, there isn’t any way to do this using WP_Query. Doing this on the event date archive is going to be extremely complicated. Well, doing it anywhere is going to be complicated because event category is not really related to the event date except through the event.

    Doing this with just the basic function that WP gives us you would need to… and I’m not sure about this

    1) Get all the even categories
    2) Get all the events in each category
    3) Somehow sort them by the event date and there isn’t any way to sort posts based on a field in another post

    What you need to be able to do is one WP_query on the events. The best way to accomplish this is to store all of the information you need to query as part of the event.

    I’ve posted several replies here about making repeaters easier to search and this is the same basic principle. You take information that you want and copy it to where it will help you the most.

    
    add_action('acf/save_post', 'make_events_filterable');
    function make_events_filterable($post_id) {
      if (get_post_type($post_id) != 'event') {
        // return;
      }
      // set up two fields to store the information
      // these need to have unique names that are different than the acf field names
      $category_field = 'event_category';
      $date_field = 'event_date';
      // delete anything that's currently stored in these two fields
      // to avoid creating duplicates
      delete_post_meta($post_id, $category_field);
      delete_post_meta($post_id, $date_field);
    
      // This is the complicated bit. You need to get the value the
      // category taxonomy field
      // and the date relationship field
      // loop through them, get the value you want to store from those locations
      // and then update the new post meta field with those values
      
          // start loop for a field
          // get value for the field
          add_post_meta($post_id, $field_name, $value, false);
          // false at the end means it can have multiple values
      
    }
    

    Now you can use these fields in WP order by clauses in the meta query to order your event posts by category and date https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

    With them in the right order you can loop through them and output new category and date headings when they change.

    Hope some of this helps.