Skip to content

Instantly share code, notes, and snippets.

@kovshenin
Created June 27, 2012 05:17
Show Gist options
  • Save kovshenin/3001645 to your computer and use it in GitHub Desktop.
Save kovshenin/3001645 to your computer and use it in GitHub Desktop.
<?php
function my_pre_user_query( $query ) {
$where = get_posts_by_author_sql( 'post' ) . " AND MONTH(post_date) = " . date( 'm', strtotime( '-1 month' ) );
$query->query_from = str_replace( get_posts_by_author_sql( 'post' ), $where, $query->query_from );
$query->query_where .= " AND post_count > 0 ";
$query->query_limit .= " LIMIT 5 ";
}
function my_magic_loop() {
// Get last month's top authors.
add_action( 'pre_user_query', 'my_pre_user_query' );
$top_authors = new WP_User_Query( array(
'orderby' => 'post_count',
'fields' => 'ID',
) );
remove_action( 'pre_user_query', 'my_pre_user_query' );
// Get last month's top authors' posts.
$top_posts = new WP_Query( array(
'post_type' => 'post',
'author' => implode( ',', $top_authors->get_results() ),
'monthnum' => date( 'm', strtotime( '-1 month' ) ),
) );
// Boom!
if ( $top_posts->have_posts() ) {
while ( $top_posts->have_posts() ) {
$top_posts->the_post();
// ...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment