// change the query before posts are retrieved from the database add_action( 'pre_get_posts', 'my_post_queries1' ); function my_post_queries1( $query ) { // not an admin page and is the main query if ( !is_admin() && $query->is_main_query() ) { // only alter the query on the home page if ( is_home() ) { // change these two values $dates_per_page = 4; // four dates to loop through (dates can have multiple posts) $top_category_id = 1; // category you want on top of same date posts $results = get_dates( $dates_per_page, '', $top_category_id); if ( $results ) { $all_post_ids = array(); foreach ( $results as $result ) { if(isset($result->IDs) && $result->IDs){ $all_post_ids = array_merge($all_post_ids ,(array) explode(',', $result->IDs)); } } $all_post_ids = array_unique($all_post_ids); if(!empty($all_post_ids)){ // don't use default pagination $query->set( 'nopaging', 1 ); $query->set( 'posts_per_page', count($all_post_ids) ); $query->set( 'post__in', $all_post_ids ); $query->set( 'orderby', 'post__in' ); $query->set( 'ignore_sticky_posts', 1 ); } else { // if no results found -> don't query for posts add_filter( 'posts_where', 'alter_posts_where' ); } } else { // if no results found -> don't query for posts add_filter( 'posts_where', 'alter_posts_where' ); } // end if($results) } } } function alter_posts_where( $where ){ return $where . ' AND 1 = 0'; } function get_dates( $dates_per_page, $limit = '', $cat = 1 ) { global $wpdb; $field = ''; $join = ''; $where = "WHERE post_type = 'post' AND post_status = 'publish'"; if( '' == $limit ) { // called from pre_get_post action lets get multiple posts with $cat on top $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $field = ", group_concat(DISTINCT ID ORDER BY tt.term_id = '" . $cat . "' DESC, post_date DESC) as IDs"; $join = "INNER JOIN {$wpdb->term_relationships} tr ON ($wpdb->posts.ID = tr.object_id) INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)"; $limit = ' LIMIT ' . (($paged -1) * $dates_per_page) . ',' . $dates_per_page; } $query = " SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(DISTINCT ID) as posts $field FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC $limit"; return $wpdb->get_results( $query ); } function next_date_link( $dates_per_page = 10, $label = 'Next Page' ){ return previous_date_link( $dates_per_page, $label, false ); } function previous_date_link( $dates_per_page = 10, $label = 'Previous Page', $prev = true ){ $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; if ( $prev ) { // limit to one previous post $count = (($paged -1) * $dates_per_page) + $dates_per_page; $limit = ' LIMIT ' . $count . ', 1'; } else { // limit to one next post $count = (($paged -1) * $dates_per_page)-1; if ( ($count+1) < $dates_per_page ) return ''; $limit = ' LIMIT ' . $count . ', 1'; } $results = get_dates( $dates_per_page, $limit ); if ( $results ) { $pagenum = ( $prev ) ? ($paged+1) : ($paged-1); return '' . $label . ''; } return ''; } // display pagination function based on twentytwelve_content_nav // uses the previous_date_link and next_date_link functions function get_index_pagination_nav( $html_id , $dates_per_page = 10) { $html_id = esc_attr( $html_id ); if ( is_home() ) : // pagination on the home page $prev = previous_date_link( $dates_per_page, 'Older »' ); $next = next_date_link( $dates_per_page, '« Newer' ); if($prev || $next) : ?>