I have following loop that show all children categories of a parent category(id = 3) The first loop i want to show every child category in a parent div (destination-tours) The second loop i want to show every post from current child category in a child div (des-item) But the result shows all posts in every parent div. So where's the bug? Here's my code Thank you for reading!

    <div id="<?php echo $child_category->term_id; ?>" class="destination-tours">
        <h1 class="tour-title"><?php echo category_description(3); ?></h1>

            $des_post = new WP_Query('post_type=tour', array('cat='.$child_category->term_id));
            while($des_post->have_posts()) : $des_post->the_post();

        <div class="des-item">
            <div class="tour-img col-md-3 col-sm-3 col-xs-3">
                <img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>" />
            <div class="tour-des col-md-7 col-sm-7 col-xs-7">
                <h2><?php the_title(); ?></h2>
                <p><?php the_content(); ?></p>
            <div class="tour-detail col-md-2 col-sm-2 col-xs-2">
                        echo get_post_meta($post->ID, 'currency')[0];
                        echo get_post_meta($post->ID, 'price')[0];
                <p><?php echo get_post_meta($post->ID, 'length')[0]; ?> day tour</p>
                <a href="<?php the_permalink(); ?>">READ MORE</a>
            <div class="clearfix"></div>

        <?php endwhile; wp_reset_postdata(); ?>
    <?php endforeach; endif;?>

You are wrongfully handling the WP_Query parameters. You can either use a query string type input or an array based one. Personally I much prefer the array type, so my example uses it:

$category_id = (int) $child_category->term_id;
$des_post_args = array(
  'post_type' => 'tour',
  'cat'       => $category_id
$des_post = new WP_Query( $des_post_args );
  • My pleasure! @Zee – Nicolai Aug 3 '15 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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