Created
May 14, 2012 10:52
-
-
Save pdewouters/2693274 to your computer and use it in GitHub Desktop.
featured posts loop with wp_reset_postdata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Filter the home page posts, and remove any featured post ID's from it. Hooked | |
* onto the 'pre_get_posts' action, this changes the parameters of the query | |
* before it gets any posts. | |
* | |
* @global array $featured_post_id | |
* @param WP_Query $query | |
* @return WP_Query Possibly modified WP_query | |
* http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/ | |
*/ | |
function itheme2_home_posts( $query = false ) { | |
// Bail if not home, not a query, not main query, or no featured posts | |
if ( ! is_home() && ! is_a( $query, 'WP_Query' ) && ! $query->is_main_query() && ! itheme2_featuring_posts() ) | |
return; | |
// Exclude featured posts from the main query | |
$query->set( 'post__not_in', itheme2_featuring_posts() ); | |
// Note the we aren't returning anything. | |
// 'pre_get_posts' is a byref action; we're modifying the query directly. | |
} | |
add_action( 'pre_get_posts', 'itheme2_home_posts' ); | |
/** | |
* Test to see if any posts meet our conditions for featuring posts. | |
* Current conditions are: | |
* | |
* - sticky posts | |
* - with featured thumbnails | |
* | |
* We store the results of the loop in a transient, to prevent running this | |
* extra query on every page load. The results are an array of post ID's that | |
* match the result above. This gives us a quick way to loop through featured | |
* posts again later without needing to query additional times later. | |
*/ | |
function itheme2_featuring_posts() { | |
if ( false === ( $featured_post_ids = get_transient( 'featured_post_ids' ) ) ) { | |
// Proceed only if sticky posts exist. | |
if ( get_option( 'sticky_posts' ) ) { | |
$featured_args = array( | |
'post__in' => get_option( 'sticky_posts' ), | |
'post_status' => 'publish', | |
'no_found_rows' => true | |
); | |
// The Featured Posts query. | |
$featured = new WP_Query( $featured_args ); | |
// Proceed only if published posts with thumbnails exist | |
if ( $featured->have_posts() ) { | |
while ( $featured->have_posts() ) { | |
$featured->the_post(); | |
if ( has_post_thumbnail( $featured->post->ID ) ) { | |
$featured_post_ids[] = $featured->post->ID; | |
} | |
} | |
set_transient( 'featured_post_ids', $featured_post_ids ); | |
} | |
} | |
} | |
return $featured_post_ids; | |
} | |
function ace_display_featured_post() { | |
$featured_args = array( | |
'post__in' => get_option( 'sticky_posts' ), | |
'post_status' => 'publish', | |
'no_found_rows' => true, | |
'posts_per_page' => 1 | |
); | |
// The Featured Posts query. | |
$featured = new WP_Query( $featured_args ); | |
// Proceed only if published posts with thumbnails exist | |
if ( $featured->have_posts() ) { | |
while ( $featured->have_posts() ) { | |
$featured->the_post(); | |
if ( has_post_thumbnail( $featured->post->ID ) ) { ?> | |
<div id="featured-post"> | |
<div class="featured-post-image"><?php the_post_thumbnail( 'featured-thumb' ); ?></div> | |
<div class="featured-post-excerpt"><?php the_excerpt(); ?></div> | |
</div> | |
<?php } | |
} | |
// Reset the post data | |
wp_reset_postdata(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment