I am using the get_pages function to display all pages within my custom post type, like this:

                $args = array(
                    'post_type'    => 'pcct_product',
                    'sort_column'  => 'post_title',
                    'menu_order'   => 'ASC'

                $pages = get_pages($args); 
                foreach ( $pages as $pagg ) {
                $option = '<option value="' . get_page_link( $pagg->ID ) . '">';
                $option .= $pagg->post_title;
                $option .= '</option>';
                echo $option;

I want to be able to display ONLY the children in this custom post type. Has anyone got any suggestions of how I can go about that?

  • 1
    Do you want to display all child pcct_product posts, or only the children of the current pcct_product post? Commented Mar 8, 2012 at 16:19
  • I want to display all child pcct_products posts Commented Mar 8, 2012 at 16:54

1 Answer 1


There are a few methods to return all child posts via get_pages().

One fairly straightforward method would be to get all of the IDs of the top-level parent posts, and then pass those IDs to the exclude parameter of get_pages():

// Get all top-level pcct_product posts;
// The 'parent' => 0 argument returns only
// posts with a parent ID of 0 - that is, 
// top-level posts
$parent_pcct_products = get_pages( array(
    'post_type'    => 'pcct_product',
    'parent'       => 0
) );
// Create an array to hold their IDs
$parent_pcct_product_ids = array();

// Loop through top-level pcct_product posts, 
// and add their IDs to the array we just
// created.
foreach ( $parent_pcct_products as $parent_pcct_product ) {
    $parent_pcct_product_ids[] = $parent_pcct_product->ID;

// Get all child pcct_product posts;
// passing the array of top-level posts
// to the 'exclude' parameter
$child_pcct_products = get_pages( array(
    'post_type'    => 'pcct_product',
    'exclude'      => $parent_pcct_product_ids,
    'hierarchical' => false
) );

(Note: untested)


Try setting hierarchical tofalse`, since we're excluding all top-level pages.

  • Passing the parent ID's into the exclude parameter removes all of the child pages as well. Commented Mar 9, 2012 at 9:27
  • Are you sure about that? It shouldn't; that's what the exclude_tree parameter is for. Commented Mar 9, 2012 at 13:50
  • 100% it is not displaying any child pages. As soon as you exclude the parent page, all the child pages are removed also. Commented Mar 9, 2012 at 14:44
  • See updated answer. Try setting hierarchical to false. Commented Mar 9, 2012 at 15:08
  • Bingo! I'll post the updated code as the get_pages() parameters also need to be in arrays. I kept getting double arrow errors with the code above. Thanks for all your help. Commented Mar 9, 2012 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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