1

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; ?>

1 Answer 1

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
    ...
);
4
  • 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? Aug 12, 2013 at 20:01
  • 1
    You're missing a closing ). You need 2 at the end - WP_Query( array( ... ) ); Aug 12, 2013 at 20:18
  • Eric, I've made another edit. If I now loop over those posts, nothing is returned? Is the taxonomy of 'categories' correct? Thanks again. Aug 13, 2013 at 10:10
  • No, it's actually category. Updated my example, sorry for that. Aug 13, 2013 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.