- This topic has 17 replies, 2 voices, and was last updated 3 years, 11 months ago by Samuel.
-
AuthorPosts
-
April 25, 2020 at 8:06 pm #1255829Samuel
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()));
April 26, 2020 at 9:48 am #1256637TomLead DeveloperLead DeveloperHi there,
What if you use
child_of
instead ofparent
?April 27, 2020 at 6:34 am #1257711SamuelI get the same result using
child_of
orpost_parent
.April 27, 2020 at 4:23 pm #1258687TomLead DeveloperLead DeveloperHow are you defining “children” of the current post? What makes them children?
April 27, 2020 at 6:07 pm #1258760SamuelApril 28, 2020 at 9:29 am #1259848TomLead DeveloperLead DeveloperStrange, 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-typeWhat if you manually input an ID of a post parent instead of using
get_the_ID()
just to test? Does it work then?April 28, 2020 at 10:28 am #1259951SamuelIt 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?
April 28, 2020 at 4:08 pm #1260366TomLead DeveloperLead DeveloperAh 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.April 29, 2020 at 6:10 am #1261079SamuelI 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.
April 29, 2020 at 9:42 am #1261573TomLead DeveloperLead DeveloperWe 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?April 29, 2020 at 10:16 am #1261622SamuelMy 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
April 29, 2020 at 4:33 pm #1262078TomLead DeveloperLead DeveloperIs
2992
the correct ID of the parent page?Is this ID also excluded in your settings? (
post__not_in
)April 29, 2020 at 4:50 pm #1262099SamuelYes, the id 2992 has two children. I don’t know where
post__not_in
would be or why it would be needed?April 29, 2020 at 5:53 pm #1262161SamuelI 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.
April 30, 2020 at 9:15 am #1263440TomLead DeveloperLead Developerpost_parent
isn’t a setting in thewpsp_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);
-
AuthorPosts
- You must be logged in to reply to this topic.