Skip Navigation

[Resolved] For Beda please (re-opening previous ticket)

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.

This topic contains 2 replies, has 2 voices.

Last updated by julieP 1 year, 2 months ago.

Assigned support staff: Nigel.

Author
Posts
#1526395

Hi Beda,

I closed this ticket: https://toolset.com/forums/topic/toolset_get_related_post-question/ but I'm not sure if I misunderstood something because I'm not getting expected results

In my scenario I need to get the post ID of the draft child post when there are multiple child posts. I set this up:-

Post types: 'my-parent-post' & 'my-child-post' in a one-to-many relationship
Parent post id is 3660
4 x child posts: 3663 (publish), 3668 (draft), 3673 (pending), 3678 (private)
View filtering for 'my-parent-post' containing shortcode [child_post] to display the post IDs of draft child post(s)
Shortcode looks like this:-

add_shortcode('child_post', 'child_post_shortcode');
function child_post_shortcode() {
    
        global $post;
	$parent_id = $post->ID;
	
        $child_id = toolset_get_related_posts( $parent_id, array( 'my-parent-post', 'my-child-post' ), 'child', 'draft' );

    return $child_id;
}

I expected to see 3668 (the ID of the draft child post) but I get this error "PHP Fatal error: Uncaught InvalidArgumentException: Limit must be a non-negative integer or -1". If I change the function to toolset_get_related_post instead, the shortcode returns 0.

Have I misunderstood what toolset_get_related_posts returns or is my shortcode wrong somewhere?

#1526991

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I'm not sure you are using the toolset_get_related_posts function correctly (very possible; the documentation and requirements are not simple).

Here is the documentation: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Note that the second argument—for the relationship—should be the relationship slug, unless this is an old Types-2 relationship that hasn't been migrated, when you pass the parent and child as an array (which you have done, probably incorrectly).

Here's an annotated example of how to use it:

$children = toolset_get_related_posts(
    $post->ID,                  // ID of origin post
    "my-parent-my-child",       // relationship slug,
    array(
        'query_by_role'     =>  'parent',   // role of origin
        'role_to_return'    =>  'child',    // role to return
        'return'            =>  'post_id',  // (optional) what to return, IDs or objects
        'limit'             =>  -1,         // (optional) limit
        'offset'            =>  0,          // (optional) offset
        'args'              =>  array(      // (optional) array of additional query arguments
            'post_status'       =>  'draft'
        ),
        'orderby'           =>  'title',    // (optional) orderby setting
        'order'             =>  'ASC',      // (optional) order direction
        'need_found_rows'   =>  false       // (optional) whether to include number of results
    )
);

Hopefully you can see what's required there.

#1527323

Yeah, that's better! Thank you 🙂