Skip to content

Instantly share code, notes, and snippets.

@niksudan
Last active August 29, 2015 14:10
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 niksudan/ae4fb59583499b9b2714 to your computer and use it in GitHub Desktop.
Save niksudan/ae4fb59583499b9b2714 to your computer and use it in GitHub Desktop.
Get posts loops [WordPress]
<?php
/**
* Using get_posts() function
*/
$custom_posts = get_posts( array(
'post_type' => 'post_type_name',
) );
$tmp_post = $post; foreach ( $custom_posts as $post ) : setup_postdata( $post );
// loop
endforeach; $post = $tmp_post;
/**
* Using WP_Query object
*/
$query = new WP_Query( array(
'post_type' => 'post_type_name',
) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
// loop
endwhile; endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment