Skip to content

Instantly share code, notes, and snippets.

@crondeau
Last active January 4, 2016 16:39
Show Gist options
  • Save crondeau/11295123 to your computer and use it in GitHub Desktop.
Save crondeau/11295123 to your computer and use it in GitHub Desktop.
This snippet pulls posts from a CPT from a specific taxonomy terms
/* This snippet is useful for displaying content in a sidebar for example. If you have
* a CPT and a taxonomy, you may want to display all the posts from a specific taxonomy.
* The example below pulls posts from a shop CPT, set to "Eat Drink" taxonomy term.
*/
<aside class="widget">
<h3>Eat &amp; Drink</h3>
<ul>
<?php
$loop = new WP_Query(array(
'post_type' => 'shop', // This is the name of your CPT
'tax_query' => array(
array(
'taxonomy' => 'type', // This is the name of your taxonomy
'field' => 'slug',
'terms' => array( 'eat-drink' ), // This is your taxonomy term
)
),
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title'
));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
<a href="<?php echo home_url(); ?>/shops/" class="button">All Shops</a>
</aside>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment