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 using the get_pages function to display all pages within my custom post type, like so:

<?php
                $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?

Many thanks

share|improve this question
1  
Do you want to display all child pcct_product posts, or only the children of the current pcct_product post? –  Chip Bennett Mar 8 '12 at 16:19
    
I want to display all child pcct_products posts –  Duncan Morley Mar 8 '12 at 16:54
add comment

1 Answer

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():

<?php
// 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)

Edit

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

share|improve this answer
    
Passing the parent ID's into the exclude parameter removes all of the child pages as well. –  Duncan Morley Mar 9 '12 at 9:27
    
Are you sure about that? It shouldn't; that's what the exclude_tree parameter is for. –  Chip Bennett Mar 9 '12 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. –  Duncan Morley Mar 9 '12 at 14:44
    
See updated answer. Try setting hierarchical to false. –  Chip Bennett Mar 9 '12 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. –  Duncan Morley Mar 9 '12 at 15:14
show 1 more comment

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.