Skip Navigation

[Resolved] toolset_get_related_posts does not return any results even though they're there

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 1 reply, has 2 voices.

Last updated by Waqar 1 year, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2521709

I am trying to: get connected posts (relationships)

This is the code:
$dwdev_exercise_id = 5455; // <= This is a valid ID in our site;
add_action( 'init', function() use ( $dwdev_exercise_id ) {
$relations = toolset_get_related_posts( $dwdev_exercise_id, 'exercise', [ 'query_by_role' => 'parent' ] );
QM::debug( $relations );

$relations = toolset_get_related_posts( $dwdev_exercise_id, 'exercise', [ 'query_by_role' => 'child' ] );
QM::debug( $relations );

$relations = toolset_get_related_posts( $dwdev_exercise_id, 'exercise', [ 'query_by_role' => 'intermediary' ] );
QM::debug( $relations );
} );

It always returns an empty array
I mean, $relations variable is always an empty array
and exercise with id=5455 has a lot of defined relations

PS. I've still got two open cases that no one seems to reply to; in one of them I've given site access

#2522909

Hi,

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

The code that you shared is missing the arguments needed for the "toolset_get_related_posts" function:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Here is an example format that makes it easy to manage all the necessary arguments:


// get related posts
$query_by_element = 5455; // ID of post to get the relationship from
$relationship = 'relationship-slug'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation
$limit = 1000; // set limit to the number of results returned
$offset = 0; // set number of results to skip
$args = array(); //nothing needed
$return = 'post_id'; // Return post IDs
$role_name_to_return = 'child'; // We want related children.
   
$get_results = toolset_get_related_posts(
	$query_by_element,
	$relationship,
	$query_by_role_name,
	$limit,
	$offset,
	$args,
	$return,
	$role_name_to_return
);

Note: Please change the 'relationship-slug' and the roles like 'parent' and 'child', as per your requirements.

We had a busy forum queue during the holidays, but your two other tickets have been replied to now. Thank you for your patience.

regards,
Waqar

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.