I'm aware there are several other posts that cover similar ground to what I'm about to ask.

I have three custom post types running, in addition to 'posts'. I want to run a loop that pulls all posts categorised under a particular category

          $args = array(
    'post_type' => 'testimonial',
    'posts_per_page' => 1,
    'tax_query' => array(
        array ( 
                'taxonomy' => 'testimonial_category',
                'field' => 'slug',
                'terms' => 'home'
$query = new WP_Query( $args );
                $postcount = 0;
            <?php if ($query->have_posts()) : ?>
                <?php while ($query->have_posts()) : $query->the_post(); ?>
                    <?php $postcount++; ?>
//loop here
<?php wp_reset_query(); ?>   

This is the code I have at the moment, not sure how I condense this into pulling in multiply post types from one category.


Just change the post_type bit to:

'post_type' => array('testimonial', 'other_post_type', 'another-post-type'),

Assuming that taxonomy is valid across all 3 post types. Otherwise you'll have to leave that out.

Why? You can pass an array to post_type field.

  • How do I say what category name to search in. 'taxonomy' => 'testimonial_category'
    – dannyw24
    Jun 18 '13 at 13:49
  • @user2478101 : ^^ Your tax_query in the question looks OK, though I didn't test it. What are you trying to do differently?
    – s_ha_dum
    Jun 18 '13 at 13:57
  • I have a taxonomy in each post type with a term in called home.I want to pull in any post that has that selected
    – dannyw24
    Jun 18 '13 at 14:00
  • I took a gamble and tried this, but it doesn't seem to pull in the other any posts from the custom post types. query_posts( array( 'post_type' => array('post', 'testimonial', 'casestudy'), 'cat' => 69, 'showposts' => 3 ) );
    – dannyw24
    Jun 18 '13 at 14:16
  • In order for this to work, the taxonomy must be identical for all. The terms, as far as I know, can't be linked across taxonomies. core.trac.wordpress.org/ticket/12269
    – GhostToast
    Jun 18 '13 at 14:17
$args = array(
    'post_type'      => array( 'testimonial', 'post' ),
    'posts_per_page' => 1,
$query = new WP_Query( $args );

Edit Note: To query for multiple post types you would add an array() of post types instead of just querying for one.

  • 1
    Samundar, can you offer a bit of an explanation for how your answer provides a solution? Sometimes the users asking a question have already been trying to solve something for hours and just getting a snippet of code can feel daunting. May 1 '20 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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