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 created a hierarchical custom post type for a gallery. The parent pages are the galleries and the child pages are the albums. I want to display the most recent albums on my sidebar (so basically the most recent child pages).

'post_parent' => 0

I only find this in the codex, but this is the opposite i want, this will only list the parent pages. Here is my code so far:

$albums = new WP_Query(array(
    'post_type' => 'gallery',
    'posts_per_page' => 6
));
share|improve this question
    
Can you please include in your question the relevant register_post_type() code? Your terminology is a bit confusing. A page is a specific post-type, and I'm unsure if you simply mean that you've created a hierarchical gallery post-type, and refer to different content on parent vs. child gallery posts, or if you have a gallery post-type and an album post-type. –  Chip Bennett Nov 25 '12 at 14:59
    
Sorry, its a bit confusing. Your first assume was right, i created a hierarchical post type called gallery. –  passatgt Nov 25 '12 at 15:04
add comment

1 Answer

up vote 3 down vote accepted

Assuming you know (or know how to get) the $id (as an integer) of the parent post, use the post_parent parameter:

$albums = new WP_Query(array(
    'post_type' => 'gallery',
    'posts_per_page' => 6,
    'post_parent' => $id
));

Edit

Based on this comment:

I don't know the parent post. I want to list the most recent child pages from ANY parent page.

While it is easy to query only parent pages (by passing 'post_parent' => 0), it is not quite so simple to query only child pages.

One method would be to query all gallery posts, loop through them, and add any that have a post_parent into another variable; then output that variable.

// Query all gallery posts
$all_albums = new WP_Query( array(
    'post_type' => 'gallery',
    'posts_per_page' => -1
) );

// Array to hold latest albums
$latest_child_albums = array();
// Latest album counter
$latest_albums_count = 0;
// If we already have 5, no need to keep looping
while ( 5 < $latest_albums_count ) {
    // Loop through each gallery post
    foreach ( $all_albums as $album ) {
        // If the current post has a parent
        if ( $album->post_parent != 0 ) {
            // Add it to the latest albums array
            $latest_child_albums[] = $album;
            // Increment the counter
            $latest_albums_count++;
        }
    }
}

// Loop through the array of latest albums
if ( $latest_child_albums->have_posts() ) : while ( $latest_child_albums->have_posts() ) : $latest_child_albums->the_post();
    // Loop content here
endwhile; endif;
// Be kind; rewind.
wp_reset_postdata();
share|improve this answer
    
I don't know the parent post. I want to list the most recent child pages from ANY parent page. –  passatgt Nov 25 '12 at 15:18
    
See updated answer... –  Chip Bennett Nov 25 '12 at 15:29
    
The edited version is a solution, but i think its a little overkill? Especially if i have hundreds of albums. Its working, so thanks. –  passatgt Nov 25 '12 at 15:39
add 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.