0

I am creating a plugin that adds two custom post types, one of which will always be a child of the other. The parent post type is for Events, the child for Performances. Before entering the Loop, I'd like to be able to find a set of Events that have Performances on a specific date. Performance date is stored as a meta field on the Performance post. The Loop would then list each Event matched, like in a standard post archive loop.

My preference is to query the Event post type, since that strikes me as easier to manage for pagination, but if there's a better implementation that simply involves querying performances, I am open to that, but I'd like to see if there's a facility for limiting a query based on a field in the child post.

1 Answer 1

1

Child posts are related to their parents (through the "post_parent" field), and not vice versa. So you would have to query your child post type first, then get the parents of all the results.

// obviously these variable names and key names might not apply,
// change them as necessary
$performances = get_posts( array(
     'meta_key' => 'the_performance_date_custom_field',
     'meta_value' => $date,
     'post_type' => 'performance' ) );

// extract the parent post ID's from each of the returned performances   
$event_ids = array_map( 
     create_function('$post','return $post->post_parent'), 
     $performances );
$event_ids = array_unique( $event_ids ); // dump any duplicate events

// and finally, get the events from their ids
$events = get_posts( array(
    'post_type' => 'event',
    'post__in' => $event_ids ) );

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.