1

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

Codition.
category1= "food" on "ait-dir-item-category" (food id is 6)
and
category2= "cate" on "ait-dir-item-category" (food id is 39)
and
post_type="ait-dir-items"

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(
            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) {
        wp_reset_query();
        $args = array('post_type' => 'ait-dir-item',
            'tax_query' => array(
              'relation' => 'AND',
                array(
                'taxonomy' => 'ait-dir-item-category',
                'field' => 'id',
                'terms' => 6
                ),
                    array(
                    '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>';
            endwhile;
         }
    }
    }

`

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

Thanks,

0

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>';
  • 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 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.