Created
May 25, 2019 16:22
-
-
Save sumnermic/2127c8645c3164495b5bbe20fbc06b1c to your computer and use it in GitHub Desktop.
WP_Query Loop for a Custom Post Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// setup query | |
$args = array( | |
'post_type' => 'custom_post_type', | |
'post_status' => 'publish', | |
'orderby' => 'date', | |
'posts_per_page' => 10, | |
'no_found_rows' => true, | |
'update_post_meta_cache' => false, | |
'update_post_term_cache' => false, | |
'fields' => 'ids' | |
); | |
// create the query | |
$custom_post_type = new WP_Query( $args ); | |
// check if query has posts | |
if( $custom_post_type->have_posts() ): | |
?> | |
<section id="custom-post-type-section"> | |
<?php | |
// loop through the query | |
while( $custom_post_type->have_posts() ): $custom_post_type->the_post(); | |
?> | |
<div class="">Looping post content goes here</div> | |
<?php | |
endwhile; wp_reset_postdata(); // stop this custom query from affecting the default query of this webpage | |
?> | |
</section> | |
<?php | |
endif; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment