Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$homeposts = yourtheme_get_posts( 'post', array( 'category_name' => 'name_of_the_category', 'posts_per_page' => 1 ), 'homepage' );
foreach ( $homeposts->posts as $homepost ) {
// process each post here
}
/**
* Returns a post object of the requested post type
*
* @param string $post The name of the post type
* @param array $params Optional parameters
* @return object A post object
*/
public function yourtheme_get_posts( $post, $params = array(), $cache = '' ) {
$return = '';
$cache_name = 'posts';
if ( ! empty( $cache ) ) {
$cache_name = '' . $cache . '_posts';
}
$return = wp_cache_get( $cache_name, 'posts' );
if ( false === $return ) {
$args['post_type'] = $post;
$args['post_status'] = 'publish';
$args['order_by'] = 'date';
$args['posts_per_page'] = 50;
$args['no_found_rows'] = true;
$args['update_post_meta_cache'] = false;
$args['update_post_term_cache'] = false;
$args = wp_parse_args( $params, $args );
$query = new WP_Query( $args );
if ( ! is_wp_error( $query ) && $query->have_posts() ) {
wp_cache_set( $cache_name, $query, 'posts', 5 * MINUTE_IN_SECONDS );
$return = $query;
}
}
return $return;
} // get_posts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment