WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post (it's called "ait-dir-items") and 2 categories there. On category archive page, I need to get posts which belong to 2 categories.

get posts from 2 categories

category1= "food" on "ait-dir-item-category" (food id is 6)
category2= "cate" on "ait-dir-item-category" (food id is 39)

I have been trying 3 ways to solve this problem. None of them is working well. please advice me how I could fix it.

1st way

`query_posts("cat=6, 39&showposts=5&post_type=ait-dir-item");
/*query_posts( array( post_type=>'ait-dir-item', 'category__and' => array(6,39), 'posts_per_page' => 12, 'orderby' => 'title', 'order' => 'DESC' ) );*/

while(have_posts()) : the_post();

echo $title = get_the_title(); echo $content = get_the_content(); endwhile; `

when I put "cat=6, 39" or "'category__and' => array(6,39)", no result found.

2nd way

` global $post;

    $tmp_post = $post;

    $args = array(
        'posts_per_page'  => 5,
        'post_type' => 'ait-dir-item',
        'category__and' => array(6, 39) // where 1, 2 is cat id
        /*'tax_query' => array(
                'taxonomy' => 'ait-dir-item-special',
                'field' => 'id',
                'terms' => 39 // taxonomy id
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : 

    $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );
    $url = $thumb['0'];

        <li class="display_special_items"><img src="<? echo $url; ?>"/><a>"><?php the_title(); ?></a>
    <?php endforeach; ?>
    <?php $post = $tmp_post; ?>
    <li class="clear">


3rd way : relation and AND

` $custom_terms = get_terms('ait-dir-item-special'); $other_custom_terms = get_terms('ait-dir-item-category');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
        $args = array('post_type' => 'ait-dir-item',
            'tax_query' => array(
              'relation' => 'AND',
                'taxonomy' => 'ait-dir-item-category',
                'field' => 'id',
                'terms' => 6
                    'taxonomy' => 'ait-dir-item-special',
                    'field' => 'id',
                    'terms' => 39

         $loop = new WP_Query($args);
         if($loop->have_posts()) {
            echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';

            while($loop->have_posts()) : $loop->the_post();
                echo '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';


I think I have little problems with these codes. it shows but duplicated and all item posts. how should I fix it?


share|improve this question
up vote 0 down vote accepted

The first two methods aren't working because the 'cat=' and 'category__and' query variables are only for the standard post type (built in) Category taxonomy conditions.

The 3rd method is nested in multiple foreach loops which could be the cause of the duplication. I think it will work like you want if you just remove both those foreach loops and let the query stand on it's own. Currently you are repeating the query for every term in both taxonomies.

Instead of using foreach, just grab the the current terms once by replacing this:

echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';

with something like this:

$term1 = get_term_by('id', 6, 'ait-dir-item-special');
$term2 = get_term_by('id', 39, 'ait-dir-item-category');
echo '<h1 style="margin-top:10px;">'.$term1->name.' and '.$term2->name.'</h1>';
share|improve this answer
thanks for your answer. do you mean remove replace the 1st gray part to 2nd part? it shows still more posts than I expect. still duplicated. :( I am so sorry to give you a bad news and really thanks for your time and help. – pulla Jun 25 '13 at 8:37
oooohhhh Andrew, you are right!! I should have read your explanation carefully. I forgot to remove "foreach loops". I have removed and it works fine. I haven't tested all of them yet. but looks like working perfect. – pulla Jun 25 '13 at 9:14

Your Answer


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.