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 have a custom post (it's called "items") and 2 categories there.
1. on category page, I need to get posts which belong to 2 categories.

get posts from category1="Food" and category2="featured"

How can I get these posts from 2 categories?

This is what I have been doing is....

I just got posts from "FOOD" like this. at the same time, I also need to get "featured" from "cate-type2".

I am new here so I have no idea how to put html, php codes.. :)


global $post;
$tmp_post = $post;
$args = array(
    'posts_per_page'  => 5,
    'post_type' => 'item',
    'tax_query' => array(
        array(
            'taxonomy' => 'cate-type1',
            'field' => 'id',
            'terms' => 39 // Food taxonomy id
        )
    )
);

$myposts = get_posts( $args );

foreach( $myposts as $post ) : setup_postdata($post); ?>

ADDITION CODE: relation and AND

so I need to get posts from
tax1 : item-special (cate id: 6)
tax2 : item-category (cate id: 39)


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

foreach ($custom_terms as $custom_term) { foreach ($other_custom_terms as $other_custom_term) { wp_reset_query(); $args = array('post_type' => 'item', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'item-category', 'field' => 'id', 'terms' => 6 ), array( 'taxonomy' => '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 this code. it shows but duplicated and all item posts. how should I fix it?

Thanks,

share|improve this question
add comment

2 Answers

up vote -1 down vote accepted

Now try this, surely it will work

query_posts("cat=1, 2&showposts=5&post_type=item");

while(have_posts()) : the_post();

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

endwhile;

share|improve this answer
    
Thanks for your quick answer. it shows all of item (custom post type) I put array(6, 39) but it doesn't work. any tips? –  pulla Jun 24 '13 at 8:12
    
Please add an explanation to your answer: why could that solve the problem? –  toscho Jun 24 '13 at 8:36
    
hello balamurugan, thanks for your time. when I put cat=6,39 it is not showing anything. I have tried others on wp-codex. but it's not showing any result when I put cat or category__and.... //query_posts("cat=6&showposts=5&post_type=ait-dir-item"); //query_posts("cat=6,39&showposts=5&post_type=ait-dir-item"); //query_posts("showposts=5&post_type=ait-dir-item"); only the last one is working but showing all of them. –  pulla Jun 24 '13 at 11:51
    
query_posts( array( post_type=>'ait-dir-item', 'category__and' => array(6,39), 'posts_per_page' => 12, 'orderby' => 'title', 'order' => 'DESC' ) ); query_posts( array( post_type=>'ait-dir-item', 'category' => 39, 'posts_per_page' => 12, 'orderby' => 'title', 'order' => 'DESC' ) ); These are not working as well when I add 'category__and', 'category'. without them, it works. but showing all of items. Thanks for your time again, please let me know if you have any idea. –  pulla Jun 24 '13 at 11:55
    
@toscho I will add how to solve my problem, when I figure it out. thanks all. –  pulla Jun 24 '13 at 12:01
show 1 more comment

You can use AND relation between tax_query

$posts = get_posts(  array( 'numberposts'   => -1,
                            'post_type'     => 'post_type',
                            'tax_query'     => array('relation'=> 'AND',
                                                        array(  'taxonomy'  => 'taxonomy_1',
                                                                'field'     => 'id',
                                                                'terms'     => 'taxonomy_id_1'
                                                                )),
                                                        array(  'taxonomy'  => 'taxonomy_id_2',
                                                                'field'     => 'id',
                                                                'terms'     => 'taxonomy_id_2'
                                                                )),
                            'meta_query'    => array(array( 'key' => 'meta_key',
                                                            'value' => 'meta_value'
                                                            ))
                                            )
                                    ); 
share|improve this answer
    
I added new codes, please see what I have done wrong. Thanks for your time and help –  pulla Jun 24 '13 at 8:38
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.