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
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