Skip Navigation

[Resolved] toolset_get_related_posts() documentation

This support ticket is created 5 years, 11 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 3 voices.

Last updated by davidS-53 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1184874

The usage example documentation for the legacy and the new function seems to be the same. It also does not show how the query should be constructed to use all the arguments.

Could you please let me know how I need to modify the code below for the query to work in the new relationships version of toolset?

Initially, I'm trying to count all 'programming' posts that are children of the '$atts[child'] post. Later I'll try and introduce some meta queries as well but this is returning a white screen.

	$query = toolset_get_related_posts(
	$atts['child'],
	'child-programming',
                array(
		// 'query_by_role' => '',
		'limit'			=> '0',
		'offset' 		=> '0',
		'args'			=> array(),
		'return'		=> 'post_id',
		'role_to_return' => 'child',
		'orderby'		=> '',
		'order '		=> '',
		'need_found_rows' => true			
		)	
	);
#1184955

Nigel
Supporter

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

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

Hi Mark

If you are getting a white screen it means you should find a fatal PHP error in your debug.log which would probably give a clue as to the specific problem.

The toolset_get_related_posts function was rewritten so that it is no longer required to pass all of the arguments (the documentation for the legacy version refers to that earlier version, rather than legacy Types relationships).

You need to provide the query_by_role, which, if you are trying to retrieve child posts would be the parent.

Most of the arguments are optional, but their use would look something like this:

$origin_id = $atts['child'];
$relationship_slug = 'breed-dog';

$child_posts = toolset_get_related_posts(
	$origin_id, // get posts connected to this one
	$relationship_slug, // in this relationship
	array(
		'query_by_role' => 'parent', // origin post role
		'role_to_return' => 'child', // role of posts to return
		'return' => 'post_id', // 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' => null // for adding meta queries etc.
	)
);

Within the array of arguments you should supply the first two (query_by_role and role_to_return), the rest can be omitted (and can be supplied in any order).

Note that we have an internal ticket to update the limit option to accept -1 (like WP_Query, to mean no limit), but currently if you want no limit you would need to specify some large number.

#1198051

Hi Nigel, thanks for your response. I've double checked that there aren't any query filters in the frontend, and added the code below:

// Filter Drill Report drill options to only those related to current boat
function drill_report_filter_query( $view_args, $view_settings, $view_id )  {
   
  if ( in_array( $view_id, array( 2781 ) ) ) {
     
    $parent_id = 63;
    $relationship_slug = 'boat-drill';

    $children = toolset_get_related_posts(
        $parent_id,
        $relationship_slug,
        array(
            'query_by_role' => 'parent',
            'limit'         => 999,
            'return'        => 'post_id',
            'role_to_return'    => 'child'
        )
    );
 
    $view_args['post__in'] = $children;
    $view_args['post__not_in'] = null;
 
  }
   
  return $view_args;

}
add_filter( 'wpv_filter_query', 'drill_report_filter_query', 101, 3 );

However, it is still showing the complete list of drills- have I missed a step somewhere? Cheers!