WordPress.org

Support

Support » How-To and Troubleshooting » Custom query for multiple categories

Custom query for multiple categories

  • neoen
    Member

    @neoen

    I use this code from Codex:

    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->term_taxonomy.term_id = 27
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->postmeta.meta_key = 'house_price'
    ORDER BY $wpdb->postmeta.meta_value ASC

    But I need to modify it for selecting posts which are in two different categories. I tried this:
    AND $wpdb->term_taxonomy.term_id IN (27,28)
    But this selects posts which are in category 27 OR in category 28. And I need to select posts which are in category 27 AND also in category 28. These examples does not work for me:
    AND $wpdb->term_taxonomy.term_id = 27,28
    or this:
    AND $wpdb->term_taxonomy.term_id = 27 AND $wpdb->term_taxonomy.term_id = 28
    All return “No posts found” But I know about posts which are in category 27 and also in category 28.

    Thank you for your help…

Viewing 6 replies - 1 through 6 (of 6 total)
  • thiskidisevil
    Member

    @thiskidisevil

    I second this question.

    Any way to add multiple values?

    i want to do the same thing

    Me too!! Any idea how this can be achieved? There is a guy who wrote a “multiple categories” search plugin widget for WordPress… I wonder if his code sheds any clues.

    http://wordpress.org/extend/plugins/multiple-category-selection-widget/

    PS. In this particular loop, I only want to show posts that appear in BOTH category 3 and category 16.

    I thought this code might work:

    <?php query_posts('cat=3&cat=16&showposts='.get_option('posts_per_page')); ?>
    <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
    <?php the_title(); ?>"><?php the_title(); ?><?php the_excerpt(); ?></a>
    <?php endwhile; ?>

    However, it shows posts in EITHER category 3 OR category 16.

    I need to do an “AND” query.

    Anyone have any idea how to achieve this?

    Try this for the query_posts() call:

    <?php query_posts('category__and=3&category__and=16&showposts='.get_option('posts_per_page')); ?>

    or possibly:

    <?php query_posts('cat=3&category__and=16&showposts='.get_option('posts_per_page')); ?>

    Thanks very much Sam!! I really appreciate your response.

    Actually I found a solution in the meantime that works… here’s the complete code for anyone else that’s interested:

    <?php $my_query_1 = new WP_query(array('category__and' => array(3,16,9))); ?>
    <?php while ($my_query_1->have_posts()) : $my_query_1->the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
    <?php the_title(); ?>"><?php the_title(); ?><?php the_excerpt(); ?></a>
    <?php endwhile; ?>

    (I declared the loop as a new variable called $my_query_1 in order to get around the problem that you can only run WP_query once on a page.)

    The above code works great for sidebar boxes to display stories of a particular combination of categories (e.g. “smartphones”, “reader contributions”, “Samsung”).

    If you want a way of adding it to a theme that doesn’t require hacking the core theme files (so you can easily upgrade/change your theme) you can use the Samsarin PHP Widget to put the code in.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom query for multiple categories’ is closed to new replies.