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 trying to build a query that lists posts of a custom type with specific custom field values. Then I need to list children of each of those posts within the post, and those children also need to be filtered by specific custom field values.

Here's my current code, which isn't working. Any help would be greatly appreciated.

Thank you!

<?php
    $args = array(
        'post_type' => 'clients-placements',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'type',
                'value' => 'Client'
            ),
            array(
                'key' => 'featured',
                'value' => 'yes'

            )
        )                       
    );
    $the_query = new WP_Query( $args ); 
    ?>

    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <?php get_template_part( 'content', 'clients' );?>

    <ul id="placements">
    <?php
        $args = array(
            'post_type' => 'clients-placements',
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'order' => 'ASC',
            'post_parent' => $post->ID,
            'meta_query' => array(
                array(
                    'key' => 'type',
                    'value' => 'Placement',
                ),
                array(
                    'key' => 'featured',
                    'value' => 'yes',

                )
            )   
        );
        $the_query = new WP_Query( $args ); 

        // The Loop
        while ( $the_query->have_posts() ) : $the_query->the_post();
            echo '<li>';
            the_title();
            the_content();
            echo '</li>';
        endwhile;


    ?>
</ul>   
    <?php endwhile; ?>
share|improve this question

2 Answers 2

up vote 2 down vote accepted

Your inner query is overwriting the outer, you've assigned both to $the_query.

share|improve this answer
    
yup just change the second $the_query to $child_query and u are good to go. –  Ijaas Nov 6 '11 at 9:31
    
Thanks! Works like a charm. Good catch. –  Griffin Dec 5 '11 at 22:10

Just change this:

  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

To this:

  <?php while ( $the_query->have_posts() ) : the_post(); ?>

Just to explain a bit more if this is the only loop you got in that page you can do it even simpler like this (without using '$the_query' at all..

Example:

<?php 
    query_posts(array( 
        'post_type' => 'portfolio',
        'showposts' => 10 
    ) );  
?>
<?php while (have_posts()) : the_post(); ?>
//Here would be the content you want to loop...
share|improve this answer
    
if you create a new WP_Query object, you have to call the_post() in that object to set up global variables properly, otherwise you're referring to an entirely different query object. –  Milo Nov 6 '11 at 2:13
    
i guess that depends if we are talking about a post or say a category... but i do get what you say :) - i didnt use WP_Query becuse this guy want a list of posts and not one post.. wouldnt this work?? --> this works great on my theme.. –  Sagive SEO Nov 6 '11 at 3:18

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.