I have created a custom post type that I want to use to allow people to add an arbitrary number of sections to the homepage of my theme. I want to loop through all posts of that type and include their contents in the page. My problem is that calling get_pages() with 'post_type' => 'home_section' seems to be returning a boolean, not an array.

Here is the code I'm using to test:

   $hp_sections = get_pages(array('post_type' => 'home_section'));
   echo '<!-- $hp_sections type: ', gettype($hp_sections), ', value: ', $hp_sections, ' -->';

When I view source on my page I get:

<!-- $hp_sections type: boolean, value:  -->

I'm pretty new to both PHP and Wordpress so I'm assuming I'm making some sort of dumb mistake. I'd like to know both what is wrong with the above code and what the recommended way to get all pages of a given type and iterate over them inside a template is.

  • 1
    From the Codex page for get_pages(): [Returns] An array containing all the Pages matching the request, or false on failure. [Emphasis mine.] Try doing var_dump( $hp_sections ); -- if it spits out "Boolean false" then there's something amiss with your request (or there aren't any valid posts to return). Also: try using get_posts() instead of get_pages(). – Pat J Nov 21 '13 at 20:58
  • @PatJ Oops, I guess I missed that second part when reading the manual. get_posts() does seem to work and I'm not sure why the analogous query is failing with get_pages(). The docs seem to indicate that get_posts() and get_pages() should be used in different situations but never really elaborate on the criteria for each. Would you consider this an appropriate place to use get_posts() over get_pages()? – Mike Deck Nov 21 '13 at 21:49
  • Yes, I'd use get_posts() instead of get_pages(). Generally the only time I'll consider using get_pages() is if I actually want Pages. Even then I might try using get_posts( array( 'post_type' => 'page' ) ); first to see if it does what I want. – Pat J Nov 21 '13 at 21:55

Historically I've had better luck using get_posts() over get_pages(). Especially in the case of a custom post type, I'd recommend using something like

$args = array(
    'post_type' => 'home_section',
$hp_sections = get_posts( $args );

instead of using get_pages().


  • 3
    To add to this, get_pages I believe only supports hierarchical post types. So unless home_section is hierarchical, it short circuits and returns false. – Eric Holmes Jun 30 '14 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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