$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Get last child of given page by ID|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Get last child of given page by ID

matteradmin9PV0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

WP_Query defaults to getting posts, not pages.

From the above reference page:

Display content based on post and page parameters. Remember that default post_type is 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far