Skip Navigation

[Resolved] How to get the intermediate post when the related posts are known

This thread is resolved. Here is a description of the problem and solution.

Problem:
A many-to-many relationship connects two post types, via an intermediary post type.

If the end posts are known, how to retrieve the intermediate post that connects them?

Solution:
Use the toolset_get_related_posts API function.

An example of retrieving the intermediate post ID would be:

$left_id = 168;
$right_id = 169;
 
// get the intermediate post(s) between these two posts
$intermediate_ids = toolset_get_related_posts( 
 
    array( 'parent' => array($left_id), 'child' => array($right_id) ), // origin post ID(s)
    'left-right', // slug of relationship
    array ('role_to_return' => 'intermediary')
);
$intermediate_id = $intermediate_ids[0];

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

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by Patrick 5 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#1179968

I am trying to use a shortcode with two shortcodes in an attribute for a view.

[wpv-view name="alle-locatie-opties-details" relid="[imgrelatieid prod='[return_post_id]' loc='[wpv-post-id]']"]

It doesn't work. What to do?

The shortcode [imgrelatieid prod='[return_post_id]' loc='[wpv-post-id]'] on its own works. Can you help?

#1180034

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Patrick

I tried many different combinations and variants with double- or single-quotes and couldn't get any to work where the wpv-view shortcode was correctly parsed when having a non-Toolset shortcode to supply a shortcode attribute.

I suggest you re-think your approach, and instead of nesting the shortcodes in the way you describe to provide attributes for the View, you use the Views API to modify the View to achieve the same effect.

So, use wpv_filter_query (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query) to modify the query generated by the View, reproducing the logic you are trying to impose with your nested shortcodes.

If you need help with that, please give a full description of how it is you are aiming to modify the View.

#1180083

I wanted a view with fields from an intermediary relationship. I coudn't get it right. So i made an view with the ID set by the View shortcode attribute "ids" as filter...

It works now, but can this be done easier?

// Add Shortcode
function img_relatieid( ) {

	global $wpdb;
	$table_name = $wpdb->prefix . "toolset_associations";
	
	$locatie = do_shortcode('[wpv-post-id]');
	$product = do_shortcode('[wpv-post-id item="$current_page"]');
	
	$relatieid = $wpdb->get_results("SELECT * FROM $table_name WHERE parent_id = $product AND child_id = $locatie");
	
	if (count($relatieid)> 0){
   
              foreach ($relatieid as $row) { 
	          return $row->intermediary_id; 
	     }

	} else {
             return 9999;
	}
}
add_shortcode( 'imgrelatieid', 'img_relatieid' );

#1180151

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The only thing I would change is instead of accessing the database table directly I would use the relationship API, namely toolset_get_related_posts (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts).

The documentation is a little opaque. I'll have to set up a test to verify how to retrieve an intermediate post ID when you have the IDs of both end posts, that's what we are aiming for, correct?

(You might be able to output fields from the intermediate posts without special code using a View that is set to query the intermediate post type if you can add Query Filters for both ends of the relationship.)

#1180765

Yes please, can you setup a test? A test to verify how to retrieve an intermediate post ID when you have the IDs of both end posts, that's what we are aiming for.

Thanks.

#1180880

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Patrick

I was able to get it working with the following code:

	$left_id = 168;
	$right_id = 169;

	// get the intermediate post(s) between these two posts
	$intermediate_ids = toolset_get_related_posts( 

		array( 'parent' => array($left_id), 'child' => array($right_id) ), // origin post ID(s)
		'left-right', // slug of relationship
      	array ('role_to_return' => 'intermediary')
	);

	error_log('intermediate_ids: ' . print_r($intermediate_ids, true));

I have a m2m relationship between "left" posts and "right" posts.

So using tooset_get_related_posts as described here will return the intermediate post ID (as an array, so the actual ID of the intermediate post would be $intermediate_ids[0]).

You'll obviously have to adapt this to your needs.

#1181401

My issue is resolved now. Thank you!