Skip to content

Instantly share code, notes, and snippets.

@aaroneaton
Last active December 16, 2015 15:19
Show Gist options
  • Save aaroneaton/5454717 to your computer and use it in GitHub Desktop.
Save aaroneaton/5454717 to your computer and use it in GitHub Desktop.
Getting one post from multiple categories
<?php
function query_features() {
global $query;
// Setup arguments for our query
$agencies = array(
'extension' => 'AgriLife Extension',
'research' => 'AgriLife Research',
'college' => 'College of Agriculture &amp; Life Sciences',
'tfs' => 'Texas Forest Service',
'tvmdl' => 'TVMDL'
);
// Get the category ids from slugs
$cat_ids = $this->get_cat_id( $agencies );
$cat_args = array(
'include' => $cat_ids,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories( $cat_args );
$slides = array();
foreach( $categories as $cat ) {
$args = array(
'showposts' => 1,
'category__in' => $cat->term_id,
'caller_get_posts' => 1
);
$post = get_posts( $args );
setup_postdata( $post );
$slides[] = array(
'img' => $this->get_thumb( $post[0]->ID ),
'caption' => $this->get_title( $post[0]->post_title ),
'permalink' => get_permalink($post[0]->ID),
'pubdate' => get_the_time( 'U', $post[0]->ID)
);
}
// Sort the slides array by 'pubdate' descending
usort($slides, array( $this, 'cmp' ) );
return $slides;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment