Skip Navigation

[Résolu] Get Related posts filtered on status

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 3 réponses, has 2 voix.

Last updated by fifthI Il y a 3 années et 6 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

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01: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

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