Skip Navigation

[Resolved] Post relationships with additional assignments

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 25 replies, has 2 voices.

Last updated by Waqar 2 months, 1 week ago.

Assisted by: Waqar.


I have a case where I already have many to many relationships like "events" and "speakers". I need to then designate one or more "host(s)" from the list of existing "speakers". What is the best way to accomplish this? Do I need to create a secondary many to many relationship?

I took a look at this ( but it only allows for a single post reference. I may run into cases where I need multiple "hosts" (selected from speakers) at an "event".

Also, I'll need to display the assigned "hosts" using PHP.




Thank you for contacting us and I'd be happy to assist.

I would recommend adding a select type custom field 'Is host', with two options 'yes' and 'no' to the intermediary post type, for this existing many-to-many relationships.

As a result, whenever an event and a speaker will be joined through the relationship, you'll have the option to select whether the speaker is also the host or not.

Note: You can add custom fields to the existing many-to-many relationship, from its edit screen at WP Admin -> Toolset -> Relationships.



Thanks Waqar, I'll give that a try. What PHP code will I need to display the list of "hosts" assignment to an event on a specific event page?


You're welcome and glad I could help.

> What PHP code will I need to display the list of "hosts" assignment to an event on a specific event page?
- In the single 'event' post page, you'll have the event ID in the PHP code.

Using the 'toolset_get_related_posts' function, you can get the array of all the related 'intermediary' post IDs from the "events-speakers" relationship:

And then you can loop through those post IDs to get the custom field values from them of the 'Is host' field and keep only the ones where the value is 'yes'. Those relationship connections will be the ones that are set as 'hosts'.


Hi Waqar,

I looked through the link you share but I'm not sure I understand the code I can use. I could not find a code example I can work from.

I decided to use a simple checkbox "is-host" to assign a host. Would something like this work:

$query = new WP_Query(

'parent' => array( $event, $speaker ),
'intermediary' => $intermediary1 )

$posts = $query->posts;
foreach ($posts as $child_post) {
if(get_post_meta($child_post->ID, 'wpcf-is-host', true) == '1') :
echo '

  • ';
    echo $child_post->post_title;
    if( get_post_meta($child_post->ID, 'wpcf-title') ):
    echo ' (' . get_post_meta($child_post->ID, 'wpcf-title', true) . ')';
    echo '
  • ';


    To troubleshoot this and suggest some code examples, I'll need to see exactly how this post relationship and the custom field, are set up in the admin area.

    Can you please share temporary admin login details, along with the link to the page, where you'd like to show the list of hosts?

    Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.


    I've sent a request from Google Drive to download the Duplicator files. Please approve it and I'll deploy this on my server.


    The access is granted. Thanks


    Hi Waqar, any update on this?


    Thank you for waiting.

    Based on the requirements, you shared here is an example of a simpler shortcode using PHP code to return the hosts:
    ( this shorcode assumes that the 'events' is the parent post, 'speakers' is the child post and the many-to-many relationship slug between them has the slug 'event-speaker'. And the 'is-host' is a checkbox type custom field added to the relationship that stores '1' as the value, when checked )

    add_shortcode('custom_get_hosts', 'custom_get_hosts_func');
    function custom_get_hosts_func() {
    	global $post;
    	// get related posts of the current post
    	$query_by_element = $post->ID; // ID of post to get relationship from
    	$relationship = 'event-speaker'; // relationship slug
    	$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
    	$limit = 1000; // defaults
    	$offset = 0; // defaults
    	$args = array(
    		'meta_key' => 'wpcf-is-host',
    		'meta_value' => '1',
    		'meta_compare' => '=',
    	); // custom field query
    	$return = 'post_id'; // We want Post ID in results
    	$role_name_to_return = 'all'; // return all relationship elements
    	$get_results = toolset_get_related_posts(
    	echo '<pre>';
    	echo '</pre>';
    	return ob_get_clean();

    When you use this shortcode ([custom_get_hosts]) on the single event page, the shortcode will print information of all the relationships which are set as the 'host'.

    Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:


    Thanks Waqar, I tried following your example and the result was in array format:

    Array (
    [0] => Array ( [parent] => 1828 [child] => 13450 [intermediary] => 13985 )
    [1] => Array ( [parent] => 1828 [child] => 12080 [intermediary] => 14001 )

    How can I show the post title and a custom field value from the child posts instead? I would like to wrap them in my existing list items.



    Thanks for the update and glad that it is working.

    Here is an example of the last part of the shortcode to generate the list of child items from the '$get_results' array

    foreach ($get_results as $get_results_item) {
    	echo '<li>'.get_the_title($get_results_item['child']).'</li>';
    return ob_get_clean();

    In this example, I've used the 'get_the_title' function ( ref: ) to get the title of the posts from IDs.

    To include custom field value(s) in the loop too, you can use the 'get_post_meta' function:


    Hi Waqar,

    I could not get the custom field value to display. I tried the following but it does not work.:

    if( get_post_meta($result_item['child'], 'wpcf-title') ):
    	echo ' <span class="ctitle">(' . get_post_meta($result_item['child'], 'wpcf-title', true) . ')</span>';

    The code works on my test website:

    foreach ($get_results as $get_results_item) {
    	echo '<li>'.get_the_title($get_results_item['child']);
    	if( get_post_meta($get_results_item['child'], 'wpcf-title') ):
    	echo ' <span class="ctitle">(' . get_post_meta($get_results_item['child'], 'wpcf-title', true) . ')</span>';
    	echo '</li>';
    return ob_get_clean();

    Please make sure that the code is using the correct variable and array names and the custom field slug.


    Hi Waqar, see what results you get on the test site with this code, where I just commented out the title of the post. I am trying to show the value custom field "wpcf-title" but it's not being displayed.

    foreach ($get_results as $get_results_item) {
      //  echo '<li>'.get_the_title($get_results_item['child']);
        if( get_post_meta($get_results_item['child'], 'wpcf-title') ):
        echo ' <span class="ctitle">(' . get_post_meta($get_results_item['child'], 'wpcf-title', true) . ')</span>';
        echo '</li>';
    return ob_get_clean();

    In my previous code,(before using intermediary post type, I have used the same 'wpcf-title' to pull the value of custom field from child post, so I know it's correct:

    if( get_post_meta($child_post->ID, 'wpcf-title') ):
    	echo ' <span class="ctitle">(' . get_post_meta($child_post->ID, 'wpcf-title', true) . ')</span>';
    LandruF8417 confirmed that the issue was resolved on 2024-05-16 15:11:10.
    This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.