WordPress.org

Ready to get started?Download WordPress

Forums

Custom query for multiple categories (7 posts)

  1. neoen
    Member
    Posted 5 years ago #

    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...

  2. thiskidisevil
    Member
    Posted 5 years ago #

    I second this question.

    Any way to add multiple values?

  3. csseur3
    Member
    Posted 5 years ago #

    i want to do the same thing

  4. danwarne
    Member
    Posted 5 years ago #

    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/

  5. danwarne
    Member
    Posted 5 years ago #

    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?

  6. Sam Bauers
    Member
    Posted 5 years ago #

    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')); ?>
  7. danwarne
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic