Why is get_pages() returning a boolean?

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:

Read More
<?php
   $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.

Related posts

1 comment

  1. 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().

    Reference

Comments are closed.