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'm looking to optimize a page where I'm using several queries (with WP Query) to retrieve similar data, and I'm considering merging these queries together then filtering for relevant data once inside a loop. In one of the loops I want to not display subpages even though they are fetched with the query. I'm thinking along the lines of this -

    $query = new WP_Query();

    $query->query( 'post_type=pages' );

    // Menu, need to remove subpages here
    while ( $query->have_posts() ) : $query->the_post();
    if (!is_page(the_post());
        $menu .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    endif; endwhile;

Not sure how to accurately use this statement though, any ideas?

Edit: Never mind, solved it using "if( $post->post_parent == 0) { }" after initiating the while loop.

share|improve this question
add comment

1 Answer

You could even try passing 'post_parent' => 0 to the query. In your case it would be

$query->query( 'post_type=pages&post_parent=0' );
share|improve this answer
    
I want to reuse the query, that's why I want to filter the subpages first inside the loop. –  Staffan Estberg Aug 6 '12 at 14:01
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.