Home Forums General Issues Meta_query key for nested fields Reply To: Meta_query key for nested fields

  • I have only one experience that could potentially relate to this. In my case I was creating a custom events calendar for a client. The client wanted to have events that could have multiple dates, for example a class that happens every week.

    They also wanted the events to show multiple times in the “Upcoming Events” list, once for each date. This is not possible with a standard WP query as WP will only return a post once in each query.

    I had a couple of choices. The first was to make them create a new event for every date. The problems with this were that it would be a PITA to use and there would not be a consistent URL for the event. Having a single event page with a consistent UEL multiple dates would be better for SEO.

    The only logical way I could provide this was to use a repeater. The main issues here were going to be having them appear in lists multiple times and how I was going to sort them.

    What I ended up doing was using child posts.

    Each event post has a repeater that allows entering start and end dates. Each event post also has other fields of data related to the post.

    I used parent/child posts. When an event is saved a child post is created for each date entered into the repeater. To the child post I also copied other fields that would be needed for searching, filtering and ordering the events.

    In addition to the above I also added actions/filters for when an event was updated to check the existing child posts and compare the dates, deleting and adding child posts as needed. There are also filters/action that happen when an event is trashed, untrashed and deleted to apply those same actions to all the child posts.

    In the admin I used a pre_get_posts filter to hide all of the child posts, the client does not even know they exist.

    On the front end I used a pre_get_posts filter for archives to only query the child posts and not the parent posts. All child post URLs redirect to the single parent event post. All the links in the archive actually pointed to the parent post, not the child post.

    This was complicated, but the only way I could find to accomplish the needed end result.

    Your case is far more complicated, but it has the same idea. You want to be able to query and sort by start/end date. You need something that has only one start date and end date.

    Another alternative:

    Save the dates and the posts they are associated with into a lookup array as a WP option.

    The array I would set up would be something like

    $option_name = array(
      // an array of post id => array pairs, the array will hold dates
      {$post_id} = array(
          //each sub array holds a start date and end date
          'start_date' => 'value',
          'end_date' => 'value'
          //each sub array holds a start date and end date
          'start_date' => 'value',
          'end_date' => 'value'
        // etc

    When I want to search I would load this option, loop over the array, find the entries that match the date I’m looking for and collect a list of post IDs then use this list of post IDs in the “post__in” argument of a WP_Query.