[Resolved] Trying To List Children of a Post using Show Posts

Home Forums Support [Resolved] Trying To List Children of a Post using Show Posts

Home Forums Support Trying To List Children of a Post using Show Posts

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #1255829
    Samuel

    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()));

    #1256637
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    What if you use child_of instead of parent?

    #1257711
    Samuel

    I get the same result using child_of or post_parent.

    #1258687
    Tom
    Lead Developer
    Lead Developer

    How are you defining “children” of the current post? What makes them children?

    #1258760
    Samuel

    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

    #1259848
    Tom
    Lead Developer
    Lead Developer

    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?

    #1259951
    Samuel

    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?

    #1260366
    Tom
    Lead Developer
    Lead Developer

    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.

    #1261079
    Samuel

    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.

    #1261573
    Tom
    Lead Developer
    Lead Developer

    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?

    #1261622
    Samuel

    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
    
    #1262078
    Tom
    Lead Developer
    Lead Developer

    Is 2992 the correct ID of the parent page?

    Is this ID also excluded in your settings? (post__not_in)

    #1262099
    Samuel

    Yes, the id 2992 has two children. I don’t know where post__not_in would be or why it would be needed?

    #1262161
    Samuel

    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.

    #1263440
    Tom
    Lead Developer
    Lead Developer

    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);

Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.