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 $all_posts = array(); $results = get_dates( $dates_per_page ); if ( $results ) { $date_sorted = array(); $i = 0; // loop through dates foreach ( $results as $result ) { $args = array( 'posts_per_page' => -1, 'year' => $result->year, 'monthnum' => $result->month, 'day' => $result->dayofmonth, 'fields' => 'ids' ); $date_posts = get_posts($args); if ( $date_posts ) { // sort date posts by category foreach ( $date_posts as $post_id ) { if ( is_object_in_term( $post_id, 'category', $top_category_id ) ) { $date_sorted[$i]['first'][] = $post_id; } else { $date_sorted[$i]['last'][] = $post_id; } } } ++$i; } // endforeach ( $results as $result ) // all post IDs with category test on top foreach ( $date_sorted as $key => $date ) { if ( isset( $date['first'] )) { $all_posts = array_merge($all_posts, $date['first'] ); } if ( isset( $date['last'] )) { $all_posts = array_merge($all_posts, $date['last'] ); } } // don't use default pagination $query->set( 'nopaging', 1 ); $query->set( 'posts_per_page', count($all_posts) ); $query->set( 'post__in', $all_posts ); $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' ); } // end if($results) } } } function alter_posts_where( $where ){ return $where . ' AND 1 = 0'; } function get_dates( $dates_per_page, $limit = '' ) { $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; if( '' == $limit ) { $limit = ' LIMIT ' . (($paged -1) * $dates_per_page) . ',' . $dates_per_page; } global $wpdb; $where = "WHERE post_type = 'post' AND post_status = 'publish'"; $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $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 ) { $count = (($paged -1) * $dates_per_page) + $dates_per_page; $limit = ' LIMIT ' . $count . ', 1'; } else { $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 ''; }