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.

How can I check two category_name values in the following query_post?

 <?php query_posts('category_name=recent-work&posts_per_page=1');
   if (have_posts()) : while (have_posts()) : the_post(); ?>

So that it reads something like this:

 <?php query_posts('category_name=recent-work&&category_name=plumbing&posts_per_page=1');
   if (have_posts()) : while (have_posts()) : the_post(); ?>

Many thanks.

EDIT:

<?php
        $querySimilarWork = new WP_Query( array(
            'tax_query' => array(
                array(
                    'taxonomy' => 'categories',
                    'field' => 'slug',
                    'terms' => array( 'recent-work', 'plumbing' )
                )
            ),
            'posts_per_page' => 4
        );
    ?>

EDIT:

If I now query this WP_query, it doesn't return anything?

<?php while ($querySimilarWork->have_posts()) : $querySimilarWork->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>   
    </li>
 <?php endwhile; ?>
share|improve this question

1 Answer 1

Don't use query_posts. Use WP_Query, and use it's tax_query.

$query = new WP_Query( array(
    ...
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array( 'recent-work', 'plumbing', 'cat3' )
        )
    ),
    'posts_per_page' => 1
    ...
);
share|improve this answer
    
Thanks Eric, I will try that now. –  SixfootJames Aug 12 '13 at 15:06
    
Hi Eric. I'm not sure where I am going wrong with this. I have read the Codex, looked at more examples on the web as well as used two different WP_Query generators to see if I am missing something but every time I implement this, I keep getting Parse error: syntax error, unexpected ';' in E:\Websites\Xampp-1.7.7\htdocs\....\content-sidebar-page.php on line 109 If delete the EDIT above, all's good and well again? –  SixfootJames Aug 12 '13 at 20:01
    
You're missing a closing ). You need 2 at the end - WP_Query( array( ... ) ); –  Eric Holmes Aug 12 '13 at 20:18
    
Super, my page is not failing now, thanks Eric. –  SixfootJames Aug 13 '13 at 8:20
    
Eric, I've made another edit. If I now loop over those posts, nothing is returned? Is the taxonomy of 'categories' correct? Thanks again. –  SixfootJames Aug 13 '13 at 10:10

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.