Site logo

Archived topic

Trying To List Children of a Post using Show Posts

17 replies · Started by Samuel on April 25, 2020

Viewing posts 1–15 of 18

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?

I'm using the normal way of selecting a parent. I've linked screenshots of the edit page and the listing page showing that it is a recognized as a child post.

The edit page: Edit Page

The list page: List Page

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?

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.

This archived topic is closed to new replies.