Skip to content

Instantly share code, notes, and snippets.

@szbl
Last active December 15, 2015 07:49
Show Gist options
  • Save szbl/5226712 to your computer and use it in GitHub Desktop.
Save szbl/5226712 to your computer and use it in GitHub Desktop.
How to pull a custom post type and filter via custom taxonomy (for WordCamp San Diego 2013).
<?php
/*
Grab the top 5 locations with content tagged as "Featured"
and "Child Care" then output them in an ordered list.
*/
$locations = get_posts(array(
'post_type' => 'szbl-location',
'posts_per_page' => 5,
'orderby' => 'menu_order',
'order' => 'asc',
'tax_query' => array(
array(
'taxonomy' => 'szbl-content-tag',
'field' => 'slug',
'terms' => array( 'featured', 'child-care' )
)
)
));
if ( count( $locations ) > 0 ) :
// save the current global post
$org_post = $GLOBALS['post'];
?>
<ol>
<?php foreach ( $locations as $location ) : ?>
<?php
$GLOBALS['post'] = $location;
setup_postdata( $location );
?>
<li id="szbl-location-<?php echo get_the_ID(); ?>">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</li>
<?php endforeach; ?>
</ol>
<?php
// reset global post query
$GLOBALS['post'] = $org_post;
setup_postdata( $org_post );
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment