Skip Navigation

[Résolu] How to specify Post status to get posts with toolset_get_related_post question

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
We are using toolset_get_related_post and would like to specify the post status of the posts this function should return us.
How can we do this?

Solution:
toolset_get_related_post will Retrieve an ID of a single related post.
That posts post_status can be defined in the function, so if you want a specific status, you would have to specify it.

You should pass 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() or added by the toolset_accepted_post_statuses_for_api filter.

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

Relevant Documentation:

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

https://developer.wordpress.org/reference/functions/get_post_statuses/

This support ticket is created Il y a 4 années et 9 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Ce sujet contient 5 réponses, a 3 voix.

Dernière mise à jour par fifthI Il y a 4 années et 9 mois.

Assisté par: Beda.

Auteur
Publications
#1520581

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?

#1520633

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?

#1520663

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' );

#1522311

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?

#1524879

Yes, that's great thank you!

#1816525
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?