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 see wordpress gives you opportunity for searching posts with exactly N categories...so i tried by myself and produced this code...

$catIDs = get_cat_ID( $cat_name='CategoryName1' );
$catIDs .= ',' . get_cat_ID( $cat_name='CategoryName2');
$catIDs .= ',' . get_cat_ID( $cat_name='CategoryName3');
echo "$catIDs </br>";

$my_query = new WP_Query( array( 'category__and' => array($catIDs) ) );
if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li>
    <a href="<?php the_permalink(); ?>"> <?php the_ID(); ?> </a>
</li>
<?php endwhile; endif; wp_reset_postdata(); ?>

if i replace the array($catIDs) with 1,2,3 (the ID of the category) this solution works....why with the variable $catIDs it doesn't? and how can i make this solution dynamic (i have a form and i would pass the data in this form -category name/ID in this case)

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Make sure you are providing correct category names into the function get_cat_ID().

<?php 
$catIDs = array();
$catIDs[] = get_cat_ID( 'CategoryName1' );
$catIDs[] = get_cat_ID( 'CategoryName2' );
$catIDs[] = get_cat_ID( 'CategoryName3' );

$my_query = new WP_Query( 
    array( 
        'category__and' => $catIDs 
    ) 
);
if( $my_query->have_posts() ) : 
    while( $my_query->have_posts() ) : $my_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"> <?php the_ID(); ?> </a>
        </li> <?php 
    endwhile; 
endif; 
wp_reset_postdata(); 
?>
share|improve this answer
    
Missed the part 'making this dynamic': You need to create a array out of form submitted category ids/ category names and do the same as above. –  Subharanjan Dec 10 '13 at 11:28
    
thanks, you solve my problem –  user3086382 Dec 10 '13 at 11:42

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.