Skip Navigation

[Resolved] Simple view in php

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 reply, has 1 voice.

Last updated by Minesh 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2796094

Hello,

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 '

  • ' . esc_html(get_the_title($event_id)) . '
  • ';
    }
    echo '';
    } else {
    echo '<p>No events found for this movie.</p>';
    }

    return ob_get_clean();
    }

    add_shortcode('movie_events', 'display_movie_events');

    #2796100

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    Hello. Thank you for contacting the Toolset support.

    What if you try to use the following code and check if that help you to resolve your issue:

    $movie_id = 999;
    $relationship_slug = 'movie-event';
    
    $related_events = toolset_get_related_posts( $movie_id, $relationship_slug, 'parent', 999, 0, array(), 'post_object', 'child');
    
    if(!empty($related_events)){
    foreach($related_events as $k=>$event):
      echo $event->post_title;
    endforeach;
    
    }
    

    More info:
    - https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts