Hi Tina
If you have a query which is "show all Bookings with a parent of the current Organizer" and there are multiple Bookings for a given Exhibitor then there is no way to say avoid duplicate exhibitors.
We need to come at this from a slightly different angle, using a View that lists Exhibitors which will loop over all of them.
In the Loop Output section we won't output anything directly. The output is going to come from a View that we nest inside this View.
The nested View will query Bookings, and in the loop output section it will output not the title of the matching booking, but of the parent Exhibitor using the id attribute. This View for Bookings needs query filters for the post parents, one to specify the Exhibitor, and one to specify the Organizer.
Unfortunately Views will only let you add one parent post filter, even though you can have multiple parent post types, and so we will need a little custom code to add a second filter to the query.
So here's my set up:
- I have a Content Template for single Organizer posts which is where I want to display Exhibitors that have booked with this organizer
- To that Content Template I add a View which queries all Exhibitor posts with no query filters required
- In the Loop Output section of that View I simply insert another, nested, View
- This View queries Bookings. Make sure the option "Don't include current page in query result" is set.
- Set the Limit to 1 (this will stop us getting duplicates of the Exhibitors)
- Add a Post relationship query filter that specifies "Select posts that are a children of the current post in the loop." This will select Bookings which are children of the current Exhibitor from the outer View.
- This is where we also need to add a filter to only include Booking posts that are children of the Organizer we are currently viewing. (I'll come back to how in a moment.)
- So this View will get Booking posts which are child posts our Organizer and the current Exhibitor we are looping through, and to return a maximum of one such post.
- If it finds a matching Booking, we want to output the title of the Exhibitor, so in the Loop Output section add [wpv-post-title id="$exhibitor"] (adjust the slug as required).
- If there is no matching booking for this Organizer-Exhibitor combo we don't want to output anything, so in the Loop Output section make sure the [wpv-no-items-found] shortcode block is empty.
Now, we will need some code to add the Organizer filter to the query. You'll need to add the following to your theme's functions.php file or using a plugin like Code Snippets.
function mod_view_query( $view_args, $view_settings, $view_id ){
if ( 220 == $view_id ) {
$oid = $view_args['post__not_in'][0]; // Organizer id
$view_args['meta_query'] = array(
array(
'key' => '_wpcf_belongs_organiser_id',
'value' => $oid,
'type' => 'CHAR',
'compare' => '='
)
);
}
return $view_args;
}
add_filter( 'wpv_filter_query', 'mod_view_query', 101, 3);
Please be sure to edit the View id (this is the id of the inner view), and the _wpcf_belongs slug.
I'm employing a little trick here. We need to pass the id of the Organizer we are concerned with, and as I assume we are viewing a single Organizer post, checking the "Do not include the current page" box in the view makes the id available in $view_args.
Let me know how you get on.