Skip Navigation

[Résolu] Get Related posts filtered on status

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

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

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

Auteur
Publications
#1819257

This is my code

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

The shortcode shows posts which are on draft or supposed to be published in the future.

#1819455

The point is that i want to show only the published posts, but in reality i get all post statuses

#1821069

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

According to the documentation (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts) post_status as an argument belongs inside the args argument, so that part would look like:

$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',
        'need_found_rows' => false, // also return count of results
        'args' => array(
            'post_status' => 'publish',
        ),
    )
);
#1821081

Thank you!!! i was just placing it in the wrong place :/
How stupid.

Thank you again