I have 2 CPT's 'movie' and 'event'. There is a relationship 1(movie) to Many(event) between both.
As a starting point of a custom code script I try to load all the events related to a movie on a single post page in a content template.
However it does not find any child event... Could you please help ?
<?php
/**
* Display movie events related to a Movie post using Toolset.
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
function display_movie_events( $atts = [] ) {
// Shortcode attributes
$atts = shortcode_atts(
array(
'movie_id' => 0, // Default movie ID (0 means get_the_ID() will be used)
),
$atts,
'movie_events'
);
$movie_id = (int) $atts['movie_id']; // Sanitize the attribute
// If movie_id is not provided in shortcode, try to get it from the current post
if ( ! $movie_id ) {
$movie_id = get_the_ID();
if ( ! $movie_id ) {
return '<p>Error: No movie ID found.</p>'; // Exit if no movie ID available
}
}
$events = array();
if (function_exists('toolset_get_related_posts')) {
$events = toolset_get_related_posts($movie_id, 'movie-event', 'child');
if ( ! is_array( $events ) ) {
return '<p>Error: Could not retrieve events.</p>'; // Handle errors from toolset_get_related_posts
}
}
ob_start();
if (!empty($events)) {
echo '<ul class="movie-events">';
foreach ($events as $event) {
$event_id = $event->ID;
echo '