Problem: I have a one-to-many post relationship set up between Trial Days and Showings. I would like to display a list of Showings from the Trials Days with a custom field date in the last few days.
Solution: You can set up a View of Trial Days, filtered by the trial date custom field. Apply your 'n' number of days to the query filter of this View. You can place this View on your site temporarily to confirm the correct Trial Days are being displayed.
Next create a View of Showings, filtered by post ID, set by a shortcode attribute "ids". In the Loop of this View, add the post title for now. You can modify the display later. Next, add this code in your child theme's functions.php file, or create a new snippet in Toolset > Settings > Custom code:
function ts_get_recent_children_func( $atts ){ $parent_view_id = 12345; $child_view_id = 56789; $relationship_slug = 'some-slug'; // do not edit below this line $parents = get_view_query_results( $parent_view_id ); $query_parents = array( 'parent' => array_column($parents, 'ID'), ); $child_ids = toolset_get_related_posts( $query_parents, $relationship_slug, array( 'parent', 1000000, 0, array(), 'post_id', 'child' ) ); $child_view_args = array( 'id' => $child_view_id, 'ids' => implode(',', $child_ids) ); $children = render_view( $child_view_args ); return $children; } add_shortcode( 'ts-get-recent-children', 'ts_get_recent_children_func' );
You must edit the parent View ID to match the numeric ID of your View of Trial Days. You must edit the child View ID to match the numeric ID of your View of Showings. You must also edit the relationship slug to match your Trial Days > Showings relationship slug. You can find that in Toolset > Relationships when you edit this relationship. Finally, insert this custom shortcode where you want to display recent Showings:
[ts-get-recent-children]
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 4 replies, has 2 voices.
Last updated by 6 years, 1 month ago.
Assisted by: Christian Cox.