Skip Navigation

[Resolved] toolset_get_related_posts does not filter by post ID

This thread is resolved. Here is a description of the problem and solution.

Problem:

Query the related posts for a given page in a short code. When I pass in get_the_ID() or even a specific Post ID, the array I get back contains all children of all posts.

Solution:

You can follow our document to setup the PHP codes with function toolset_get_related_posts().

Relevant Documentation:

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

This support ticket is created 4 years, 12 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/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by Russ 4 years, 12 months ago.

Assisted by: Luo Yang.

Author
Posts
#1721399

I am trying to: Query the related posts for a given page in a short code. When I pass in get_the_ID() or even a specific Post ID, the array I get back contains all children of all posts.

Link to a page where the issue can be seen:

I expected to see: The children of just one post.

Instead, I got: All children of all posts.

This is the query I'm making. This does return posts, but not just the children of the parent post no matter what I put in the ID argument.

$gallery_images = toolset_get_related_posts(
get_the_ID(),'post-gallery-image',

// Additional arguments.
[
// Specify the role to query by and to return.
// Notice that we can use "other" in this situation.
'p' => '38639',
'query_by_role' => 'parent',
'role_to_return' => 'child',

// Ordering
'meta_key' => 'wpcf-order',
'orderby' => 'meta_value',
'orderby' => 'title',
'order' => 'ASC'
]
);

#1721533

Hello,

Please check our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
The function "toolset_get_related_posts" does not support "p" parameter.

In your case, you can try with WP_Query, see our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#new-approach
It should be able to support all standard WordPress parameters:
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

#1721861

I only added the p parameter because the prescribed method did not work.

$gallery_images = toolset_get_related_posts(
get_the_ID(),'post-gallery-image',

Even when I replace get_the_ID() with a specific post ID, I still get the same result.

#1721867

I have restored the query to my original attempt, which was essentially copy/pasted from the support document you linked. The result is exactly the same.

$gallery_images = toolset_get_related_posts(
'38639','post-gallery-image',

// Additional arguments.
[
// Specify the role to query by and to return.
// Notice that we can use "other" in this situation.
'query_by_role' => 'parent',
'role_to_return' => 'child',

// Ordering
'meta_key' => 'wpcf-order',
'orderby' => 'meta_value',
'orderby' => 'title',
'order' => 'ASC'
]
);

#1722375

It is a custom codes problem, please provide a test site with the same problem, also point out:
- The problem page URL, where I can check the result in front-end
- Where I can edit your custom PHP codes

I need a live website to test and debug, private message box enabled.

#1722401

wp-content/plugins/fer-galleries/fer-galleries.php is where the file and query are located.

#1722417

I have done below modifications in your website:
1) Edit the PHP codes of wp-content/plugins/fer-galleries/fer-galleries.php, line 23~24

			//'38639','post-gallery-image',
			get_the_ID(),'post-gallery-image',

line 40:

		var_dump($gallery_images);

2) Edit the post you mentioned above:
hidden link

in section "Gallery Images", connect to a new "Gallery Images" post

Test it in front-end:
hidden link

I can see the result works fine:

array(1) { [0]=> int(38666) }

Can you confirm it? thanks

#1722421

My mistake, you are correct, the function is returning the correct posts. The issue was in my WP loop syntax. Thank you!