PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to use multiple post_type in WP_Query?

 July 08, 2022     advanced-custom-fields, php, posts, wordpress     No comments   

Issue

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>

Solution

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:

  • WP_Query


Answered By - Raunak Gupta
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing