Skip to content

Instantly share code, notes, and snippets.

@michaelschofield
Last active August 29, 2015 14:05
Show Gist options
  • Save michaelschofield/f706f809eaa733d2e83e to your computer and use it in GitHub Desktop.
Save michaelschofield/f706f809eaa733d2e83e to your computer and use it in GitHub Desktop.
Loop any Post-Type in Specific Category
<?php
// Fetch all posts, regardless of post-type, in "my_category".
$args = array(
'post_type' => 'any',
'category_name' => 'my_category'
);
// Call the $args ^ in a new WP_Query().
$the_query = new WP_Query( $args );
// The default loop is `if ( have_posts() ) : while ( have_posts() ) : the_post();`
// but after changing the query, we have to add $the_query-> before each hook.
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
/* Close the PHP tag */ ?>
<!-- The <article> post HTML here. Pretend. -->
<?php // More PHP
endwhile; // This closes the `while( $the_query->have_posts() )` condition.
endif; // This closes the `if( $the_query-> have_posts() )` condition.
// When you modify the loop, you have to "reset" it - otherwise your modifications might
// mess up what WP does out of the box.
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment