• Hello everybody, one more advanced question.

    i have 2 groups of categories, one is list of cities and 1 is OFFERS, NEWS, TIPS.

    I have some queries in sideabar in 3 groups news, offers, tips but i want when i click on some city, NY for example to show me only news that have selected NY category and NEWS category.

    is it possible.
    when i use cat=x,y shows me all posts from both categories, and i need to show me only posts that have selected category x and y not only x or only y.

    thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • well.. It is possible to do in several ways, one of which is the in_category tag, but I think the best thing would be to order that FROM THE ROOT, by applying the right categories and tags to each post, and then query also tags in combination to categories (if you have a LOT of cities) , or if you have not so many, separate the categories in a hierarchy.
    But first look at the in_category tag..

    <?php
    $recent = new WP_Query("cat=X,Y"); while($recent->have_posts()) : $recent->the_post();
    
    	if (in_category(X) && in_category(Y)) { ?>
    		<div class="myclass">
    			<h3><?php the_title(); ?></h3>
    			<?php the_content(); ?>
    		</div>
    <?php }; ?>
    <?php endwhile; ?>

    Now, If you want to add a limit to the number of posts :

    <?php
    $recent = new WP_Query("cat=X,Y"); while($recent->have_posts()) : $recent->the_post();
    
    static $limitcount = 0;
    
    if ($limitcount == "1") { break; }
    else {
    	if (in_category(X) && in_category(Y)) { ?>
    		<div class="myclass">
    			<h3><?php the_title(); ?></h3>
    			<?php the_content(); ?>
    		</div>
    		<?php $limitcount++;
    	};
    }; ?>
    <?php endwhile; ?>

    you can also use
    if ($counter == "2") { break; }
    to set another limit.
    and
    if (in_category(X) && in_category(Y) && in_category(Z)) if you want to cross check more than 2 categories

    Thread Starter zedesino

    (@zedesino)

    hello krembo99,
    i tryed your solution but doesnt work. I want to show this posts when i’m viewing page in wp, so i’m not in_category and doesnt work.
    but thanks anyway for time you spent

    Thread Starter zedesino

    (@zedesino)

    i founded faster than i was thinking
    query_posts(array(‘category__and’ => array(3,20)));

    here

    The solution works if you implement it the right way…
    The only thing is , that maybe I did not understood what exactly you need.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘query posts from 2 categories’ is closed to new replies.