Skip Navigation

[Resolved] Getting a list of related posts

This support ticket is created 2 years, 5 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by andyS-5 2 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#2206897

Tell us what you are trying to do?
Modify code that used to work with a new many-to-many relationship. The two snippets listed below still generate results, but the first snippet shows double the results - half of which link back to the study location itself.

Old code for listing a study location's diploma titles:

$child_posts = types_child_posts('pairing');
echo '<h2>Diploma titles validated at this centre<sup>1</sup></h2>';
echo '

    ';
    foreach ($child_posts as $child_post) {
    $diploma = wpcf_pr_post_get_belongs($child_post->ID, 'diploma-title');
    $title = get_post($diploma);
    echo '

  • <a href="' . get_permalink($diploma). '">' . $title->post_title . '</a>
  • ';
    }
    echo '

';

Old code for listing where diploma titles are available (updating to new intermediate post appears to still work:

$child_posts = types_child_posts('pairing');
echo '<h2>The centres listed below are validated to run this diploma<sup>1</sup>. Please click on the college name to go to their contact page</h2>';
echo '

    ';
    foreach (array_reverse($child_posts) as $child_post) {
    $location = wpcf_pr_post_get_belongs($child_post->ID, 'study-location');
    $title = get_post($location);
    echo '

  • <a href="' . get_permalink($location). '">' . $title->post_title . '</a>
  • ';
    }
    echo '

';

New code needed as I don't want to use views, just modify existing single post templates for two related CPTs.

Is there any documentation that you are following?
Toolset Types API

Is there a similar example that we can see?
I don't know of one

What is the link to your site?
hidden link - this page is fine - it shows the study locations. When you click through to a single location, you'll see the issue.

Many thanks
Andy

#2207647

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Andy,

Thank you for contacting us and I'd be happy to assist.

Parts of the code snippets that you've shared have been stripped out by the forum's editor, but, the updated function to get the related post connections for a post, is the "toolset_get_related_posts" function:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

For example, suppose, you have a relationship between "Posts" and "Pages", with the relationship slug "post-page", where "post" is parent and "page" is a child.

To get the related "pages" from the current post, the code would look like this:


// get related pages from the current post
$query_by_element = $post_id; // ID of post to get relationship connections from
$relationship = 'post-page'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
$limit = 1; // limit set to one as we just need to check if related post exists or not
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'child'; // We want children.
 
$get_results = toolset_get_related_posts( $query_by_element, $relationship, $query_by_role_name, $limit, $offset, $args, $return, $role_name_to_return );
 
if(!empty($get_results)) {
    foreach ($get_results as $get_result ) {
        // the ID of related posts ID is available in $get_result
    }
}

I hope this example helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2208179

Hi Waqar,

Thanks for your help. I've added the code - changing the relationship slug and I get the following error:

Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /srv/users/serverpilot/apps/laser/public/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php:246 Stack trace: #0 /laser/public/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php(177): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(Array, 'parent') #1 /srv/users/serverpilot/apps/laser/public/wp-content/plugins/types/vendor/toolset/toolset-common/inc/public_api/m2m.php(110): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct(NULL, 'study-location-...', Array) #2 /srv/users/serverpilot/apps/laser/public/wp-content/themes/laser2019/single-study-location.php(98): toolset_get_related_posts(NULL, 'study-location-...', 'parent', 1, 0, Array, 'post_id', 'child') #3 /srv/users/serverpilot/apps/laser/public/wp-includes/class-wp-ho in /srv/users/serverpilot/apps/laser/public/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php on line 246

Can you see what might cause that error?

Best wishes
Andy

#2208895

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

The code snippet that I shared was just an example and an error message like this can appear if the current page/post ID is not provided in the first line through the "$post_id" variable.

Can you please make sure that you're providing the current page/post ID (i.e. the ID of the page/post from which you want to get a related post) is provided through the "$post_id" variable?

In case the issue still persists, you're welcome to share a screenshot of exactly where and how you've included this code.

#2209281

Thanks Waqar,

Here's my code so far - I've added the post ID of the current post:

global $post;
$post_id = $post->ID;

$query_by_element = $post_id; // ID of post to get relationship connections from
$relationship = 'study-location-diploma'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
$limit = 1; // limit set to one as we just need to check if related post exists or not
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'child'; // We want children.
  
$get_results = toolset_get_related_posts( $query_by_element, $relationship, $query_by_role_name, $limit, $offset, $args, $return, $role_name_to_return );
  
if(!empty($get_results)) {
echo $post_id;
    foreach ($get_results as $get_result ) {
        // the ID of related posts ID is available in $get_result
echo the_title();
    }
}

It echoes the current post ID and the current post title for some reason and no the titles of the related posts. See hidden link just below the list generated by the old code.

Thanks
Andy

#2210515

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for sharing this and can you please update your code slightly, so it becomes:


global $post;
$post_id = $post->ID;

$query_by_element = $post_id; // ID of post to get relationship connections from
$relationship = 'study-location-diploma'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
$limit = 9999; // limit set to one as we just need to check if related post exists or not
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'child'; // We want children.

$get_results = toolset_get_related_posts( $query_by_element, $relationship, $query_by_role_name, $limit, $offset, $args, $return, $role_name_to_return );

if(!empty($get_results)) {
	echo '<ul>';
	foreach ($get_results as $get_result ) {
		// the ID of related posts ID is available in $get_result
		echo '<li>'.get_the_title($get_result).'</li>';
	}
	echo '</ul>';
}

Please note how this code is now getting the title from the related post's ID available in "$get_result" and not from the current post.

#2210673

My issue is resolved now. Thank you!

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