Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
+1 for showing me the wp_list_pluck() function. Nice. Never knew about that one before. –  GhostToast May 30 '13 at 21:16
    
@GhostToast Yeah, that’s really handy. :) –  toscho May 30 '13 at 21:18
    
When I run this code, I get child pages as well as top level ones. I've checked a couple of times to confirm. get_pages is returning all pages, not just the top level pages-- the post_parent === 0 ones. –  s_ha_dum May 30 '13 at 21:34
    
@s_ha_dum Indeed, thanks. Fixed now. –  toscho May 30 '13 at 21:38

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.