Is the toolset_get_related_post shortcode supposed to be able to retrieve the related post ID irrespective of the related post's status? Or does it only retrieve the ID if the status is not private?
There is no such shortcode in Toolset, toolset_get_related_post is an API function to be used in PHP.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
It is supposed to Retrieve an ID of a single related post.
This means it gives you the ID of the post (single) that is related to the post you pass in the function.
post_status can be defined in the function, so if you want a specific status, you would have to specify it
It's an array of post status values or a string with one or more statuses separated by commas.
The passed statuses need to be among the values returned by get_post_statuses() (https://developer.wordpress.org/reference/functions/get_post_statuses/) or added by the toolset_accepted_post_statuses_for_api filter.
Does this help?
Sorry I meant function not shortcode!
If I use this will it retrieve the ID of the related post irrespective of post status?
Re specifying the post status, I did notice this in the docs before I raised this ticket but I don't understand how to incorporate it into the function and I've never come across any examples of it in the forum. If this were the basic code in my cred_save_data hook, how do I include the post status please?
$variable = toolset_get_related_post($post_id, 'the-relationship-slug' );
As an Array of post status values, or a string with one or more statuses separated by commas
hence:
array("status","status1")
or
'status,status1'
Example:
$role_name_to_return = '';//parent or child
$args = array();// see args array - Additional query arguments.
$post_status = 'publish,draft';//you can only use those from https://developer.wordpress.org/reference/functions/get_post_statuses/ or passed to toolset_accepted_post_statuses_for_api
$writer = toolset_get_related_post( $post_id, array( 'one-side', 'other-side' ), $role_name_to_return, $args, $post_status );
Does that help?
Yes, that's great thank you!
function get_lessons_for_speaker_func(){
ob_start();
global $post;
$speaker_id = $post->ID;
$relationship_slug = 'lesson-speaker';
$child_posts = toolset_get_related_posts(
$speaker_id , // get posts connected to this one
$relationship_slug, // in this relationship
array(
'query_by_role' => 'child', // origin post role
'role_to_return' => 'parent', // role of posts to return
'return' => 'post_object', // return array of IDs (post_id) or post objects (post_object)
'limit' => 999, // max number of results
'offset' => 0, // starting from
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish',
'need_found_rows' => false, // also return count of results
'args' => null // for adding meta queries etc.
)
);
$arr_m = array_map(function($o) { return "<li><a href='" . get_permalink($o->ID). "'>$o->post_title</a></li>"; }, $child_posts);
$output_string = join(", ", $arr_m);
ob_end_clean();
return "<ul>$output_string</ul>";
}
add_shortcode('get_lessons_for_speaker','get_lessons_for_speaker_func');
How do i filter only for posts with "publish" status?