get all page IDs from wp_list_pages

I am looking for a way to output all page IDs from the following menu:

<?php wp_list_pages('depth=1&exclude='3,5,11')); ?>

Only top level pages needed, therefore the ‘depth=1’.

Any help appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

wp_list_pages() is for formatted markup. Use get_pages() and wp_list_pluck():

$pages = get_pages(
    array (
        'parent'  => 0, // replaces 'depth' => 1,
        'exclude' => '3,5,11'
    )
);

$ids = wp_list_pluck( $pages, 'ID' );

$ids holds an array of page IDs now.

Method 2

You can do this with WP_Query:

$args = array(
  'post_type' => 'page',
  'post_parent' => 0,
  'fields' => 'ids',
  'posts_per_page' => -1,
  'post__not_in' => array('3','5','11'),
);
$qry = new WP_Query($args);
var_dump($qry->posts);

As you can see from the var_dump, $qry->posts is an array of IDs

Reference:

http://codex.wordpress.org/Class_Reference/WP_Query

Method 3

$pages = get_pages(
    array (
        'parent'   => 0, /* it gives only child of current page id */
        'child_of' => 'parent or page_ID' /* Return child of child for page id. */
    )
);
$ids = wp_list_pluck( $pages, 'ID' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x