Support » Fixing WordPress » Query custom post type and category

  • I have three different custom post types that all share the same set of categories. I need to set up a loop that pulls up a particular post type, but then also a category. So for example tell it to find all posts under the custom post type “Pineapple”, and also all posts under the category “Dog Sledding”.

    But I want to to show posts if they are in either, not necessarily both. Did I explain that ok? I have this right now:

    <?php $paged=(get_query_var('paged')) ? get_query_var('paged') : 1; query_posts(array('post_type'=>pineapple, 'category' => 'dog-sledding', 'showposts'=>10, 'posts_per_page' => 1, 'caller_get_posts' => 1, 'paged' => $paged )); ?>

    But I think that will only show posts if they are in both the post type and category.

    Does anyone know how to can adjust it so that the loop will pull up all posts under a post type, and also all posts under a category? Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Bump. Anyone?

    Moderator justingreerbbi


    Forum Moderator

    Use category_name is instead of category

    Right now it is puling from just pulling from post_type..

    This can all be done using shortcode using Custom Post Type Shortcode..

    I am not sure that this can be done using query_posts. However, you can use a sql statement to get the posts. Here is an example:

    $sql = "
    SELECT p.*
    FROM bmc_posts p
    JOIN bmc_term_relationships tr ON p.ID = tr.object_id
    JOIN bmc_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category')
    JOIN bmc_terms t ON (tt.term_id = t.term_id AND = 'Dog Sledding')
    WHERE post_status = 'publish'
    AND post_type = 'post'
    UNION (SELECT pp.*
    FROM bmc_posts pp
    WHERE pp.post_status = 'publish'
    AND pp.post_type = 'pineapple')
    ORDER BY post_date ASC
    $my_posts = $wpdb->get_results($sql);
    foreach ($my_posts as $post) {
          echo "<h2>TYPE:$post->post_type ";the_title();echo '</h2>';

    I like that idea vtxyzzy! I plugged that into my theme file. How do I go about echoing the title and excerpt of the posts? This isn’t actually pulling anything up yet. And thank you for your help!

    It should be showing the post type and title of each post. If nothing is showing, chances are that either the term ( or the post type (pp.post_type) are not correct. Once you get that working, you can use the_excerpt() to show the excerpt.

    Hm. I just can’t make it work. Is there a difference between categories and terms? Because I’m trying to loop through posts by a particular category, not taxonomy term. Could that be why?

    ‘category’ is a built-in WP taxonomy in the terms table, so it has terms. If you added a custom taxonomy, you need to use its name instead of ‘category’.

    Also, check the spelling and capitalization of everything – it must match exactly.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Query custom post type and category’ is closed to new replies.