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'
]
);
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.
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'
]
);
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.
wp-content/plugins/fer-galleries/fer-galleries.php is where the file and query are located.
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
My mistake, you are correct, the function is returning the correct posts. The issue was in my WP loop syntax. Thank you!