admin管理员组文章数量:1130349
I've got the following code- I'm trying to get the last child page of a page with ID 4117. Here's my code thus far:
// WP_Query arguments
$args = array(
'post_parent' => '4117',
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'menu_order',
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach($posts as $post) {
echo $post->post_title;
}
But it doesn't appear to do anything. Any clues as to what my issue may be?
I've got the following code- I'm trying to get the last child page of a page with ID 4117. Here's my code thus far:
// WP_Query arguments
$args = array(
'post_parent' => '4117',
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'menu_order',
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach($posts as $post) {
echo $post->post_title;
}
But it doesn't appear to do anything. Any clues as to what my issue may be?
Share Improve this question asked Nov 6, 2018 at 21:23 warm__tapewarm__tape 611 silver badge11 bronze badges1 Answer
Reset to default 0WP_Query defaults to getting posts, not pages.
From the above reference page:
Display content based on post and page parameters. Remember that default
post_typeis only set to display posts but not pages.
This code:
// WP_Query arguments
$args = array(
'post_parent' => '4117',
'post_type' => 'page',
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'menu_order',
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach ( $posts as $post ) {
echo $post->post_title;
}
...should do what you want.
I've got the following code- I'm trying to get the last child page of a page with ID 4117. Here's my code thus far:
// WP_Query arguments
$args = array(
'post_parent' => '4117',
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'menu_order',
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach($posts as $post) {
echo $post->post_title;
}
But it doesn't appear to do anything. Any clues as to what my issue may be?
I've got the following code- I'm trying to get the last child page of a page with ID 4117. Here's my code thus far:
// WP_Query arguments
$args = array(
'post_parent' => '4117',
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'menu_order',
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach($posts as $post) {
echo $post->post_title;
}
But it doesn't appear to do anything. Any clues as to what my issue may be?
Share Improve this question asked Nov 6, 2018 at 21:23 warm__tapewarm__tape 611 silver badge11 bronze badges1 Answer
Reset to default 0WP_Query defaults to getting posts, not pages.
From the above reference page:
Display content based on post and page parameters. Remember that default
post_typeis only set to display posts but not pages.
This code:
// WP_Query arguments
$args = array(
'post_parent' => '4117',
'post_type' => 'page',
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'menu_order',
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach ( $posts as $post ) {
echo $post->post_title;
}
...should do what you want.
本文标签: wp queryGet last child of given page by ID
版权声明:本文标题:wp query - Get last child of given page by ID 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749201633a2331900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论