• Resolved nospario

    (@nospario)


    Hi,

    I want to return posts dependant on the categories they are in, is this possible?

    For example:

    post1 is associated with 2 categories, category_name1 and category_name2

    post2 is associated with 2 categories, category_name1 and category_name3

    post3 is associated with 2 categories, category_name2 and category_name3

    I only want to return post1 and not post2 or post3 querying only on category_name.

    I’ve tried this but it’s not working as I had expected.

    //The Query
    			$cat1 = get_cat_ID($category_name1);
    			$cat2 = get_cat_ID($category_name2);
    			$args = array('cat' => $cat1 . ',' .$cat2);
    			query_posts($args);
    			//The Loop
    			if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    				<?php the_content(); ?>
    			<?php
    			endwhile;
    			endif;
    			//Reset Query
    			wp_reset_query();

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You want use “category__and” (double-underlines) e.g.
    array( ‘category__and’ => array( #, #) )

    Thread Starter nospario

    (@nospario)

    Thanks transom.

    That works when I hard code the values in but not when I use the variables. Any idea what I’m doing wrong?

    query_posts( array( 'category__and' => array($cat1.','.$cat2)));

    array($cat1, $cat2)

    not joined together as a string

    Thread Starter nospario

    (@nospario)

    That doesn’t seem to do it but I found this post which has helped me sort it.

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Querying Posts Using Categories’ is closed to new replies.