Skip to content

Instantly share code, notes, and snippets.

@sumnermic
Created May 25, 2019 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sumnermic/2127c8645c3164495b5bbe20fbc06b1c to your computer and use it in GitHub Desktop.
Save sumnermic/2127c8645c3164495b5bbe20fbc06b1c to your computer and use it in GitHub Desktop.
WP_Query Loop for a Custom Post Type
<?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