JakeFromStateFarm JakeFromStateFarm - 2 months ago 6
PHP Question

How do I use multiple post_types with WP_Query?

I have created custom fields, and am using WP_Query to output them. I have a "visibility section" and "credibility section", with custom post types for each set up in the ACF plugin. My visibility section is working perfectly, but I need to figure out how to add my credibility post type to the array argument, so that I can output my posts in that bottom section. I don't think I can just add another post_type => 'credibility_section' to it...

<?php
$args = array(
'post_type' => 'visibility_section'
//Can I add 'post_type' => 'credibility_section' here?
);

$query = new WP_Query( $args );
?>

<section class="row visibility-content"><!-- Start visibility section -->
<div class="container">
<h3 class="text-xs-center m-b-3">Visibility</h3>
<div class="col-md-6">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<p><?php the_field( 'visibility_left_column' ); ?></p>
</div>
<div class="col-md-6">
<?php if( get_field( 'visibility_image' ) ): ?>
<img src="<?php the_field( 'visibility_image'); ?>" />
<?php endif; ?>
</div>
<div class="col-md-12">
<p><?php the_field( 'visibility_bottom' ); ?></p>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
</section>

<section class="row cred-content"><!-- Start Credibility section -->
<div class="container">
<h3 class="text-xs-center m-b-3">Credibility</h3>
<div class="col-md-12">

</div>
</section>

Answer

You can pass multiple post_type using array in WP_Query argument.

$args = array(
    'post_type' => array( 'visibility_section', 'credibility_section')
);
$query = new WP_Query( $args );


Reference: