Archived topic
Trying To List Children of a Post using Show Posts
17 replies · Started by Samuel on April 25, 2020
I have a custom post type and I'm trying to list the children of a specific post using show posts. I'm using this line of code, but for some reason it does not show the children of the current post. Instead it lists all the other posts of this type. I can't figure out what I'm doing wrong?
wpsp_display($show_posts_id, array('post_parent' => get_the_ID()));
Hi there,
What if you use child_of instead of parent?
I get the same result using child_of or post_parent.
How are you defining "children" of the current post? What makes them children?
Strange, looked around quite a bit and both should work:
https://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/
https://wordpress.stackexchange.com/questions/262090/display-a-list-of-child-posts-on-parent-posts-of-a-custom-post-type
What if you manually input an ID of a post parent instead of using get_the_ID() just to test? Does it work then?
It gives the same result. I'm completely baffled and not sure how to debug. Is there is easy way to dump the query args posts is using to see what the problem is?
Ah I think I see the issue.
You'll need to make this change to the plugin: https://github.com/tomusborne/wp-show-posts/commit/286caf1164db8b6b6f38b85d3a011b519a27f4de
Then you should be able to do this:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
if ( 123 === $settings['list_id'] ) {
$args['child_of'] = get_the_ID();
}
return $args;
}, 10, 2 );
You just need to change 123 to the ID of the list you're targeting.
I made those changes to the plugin and then changed my code to just run: wpsp_display($show_lessons_id);
Then I added your code above and I am still getting the same result. I feel like there's something here obvious I am missing but I just don't see it.
We can debug the query like this:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
if ( 123 === $settings['list_id'] ) {
$args['child_of'] = get_the_ID();
}
var_dump($args);
return $args;
}, 10, 2 );
Are you seeing child_of with the correct ID in the output?
My Wordpress skills are rusty so not sure if this is correct, but this is the output:
array (size=7)
'order' => string 'asc' (length=3)
'orderby' => string 'none' (length=4)
'post_type' => string 'courses' (length=7)
'posts_per_page' => int 99
'post_status' =>
array (size=1)
0 => string 'publish' (length=7)
'post__not_in' =>
array (size=1)
0 => int 2992
'child_of' => int 2992
Is 2992 the correct ID of the parent page?
Is this ID also excluded in your settings? (post__not_in)
Yes, the id 2992 has two children. I don't know where post__not_in would be or why it would be needed?
I don't know if this helps, but to test I ran this and the result was correct. It correctly displayed the children:
$lessons = get_posts(array('post_parent' => $post->ID, 'post_type' => 'courses', 'post_status' => 'publish', 'orderby' => 'menu_order'));
echo '<div><ul>';
foreach ($lessons as $lesson) {
echo '<li><a ' . $css_class . ' href="' . get_permalink($lesson->ID) . '">' . $lesson->post_title . '</a></li>';
}
echo '</ul></div>';
And then I ran this code right after it and the result was not correct. It showed every post of this post type except the current post:
wpsp_display($show_lessons_id, array('post_parent' => $post->ID, 'post_type' => 'courses', 'post_status' => 'publish', 'orderby' => 'menu_order'));
I'm stumped. Different results but the exact same query args.
post_parent isn't a setting in the wpsp_display function, which is why we went the filter route.
What you want to do is compare the query args.
So what I showed above: https://generatepress.com/forums/topic/trying-to-list-children-of-a-post-using-show-posts/#post-1261573
vs doing this in your test:
var_dump($lessons);