function next_week_link( $label = 'Next Week' ) {
echo get_week_link( $label, 'next' );
}
function previous_week_link( $label = 'Previous Week' ) {
echo get_week_link( $label, 'previous' );
}
function get_week_link( $label, $next_or_previous = '' ) {
$archive_month = get_this_week( $next_or_previous );
if ( $archive_month ) {
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
if ( $next_or_previous == 'next' ) {
--$paged;
}
if ( $next_or_previous == 'previous' ) {
++$paged;
}
return '<a href="' . get_pagenum_link( (int)$paged ) . '">' . $label . '</a>';
}
return '';
}
function get_this_week( $next_or_previous = '' ) {
$arc_week = array();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
if ( $next_or_previous == 'next' && $paged == 1 ) {
return $arc_week;
} elseif ( $next_or_previous == 'next' && $paged > 1 ) {
--$paged;
}
if ( $next_or_previous == 'previous' ) {
++$paged;
}
$limit = ( $paged > 1 ) ? ' LIMIT ' . ( $paged - 1 ) . ',1' : " LIMIT 1";
global $wpdb;
$where = get_posts_by_author_sql( 'post' );
//$where = "WHERE post_type = 'post' AND post_status = 'publish'";
$join = '';
$order = 'DESC';
$week = _wp_mysql_week( '`post_date`' );
$query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit";
$arcresults = $wpdb->get_results( $query );
if ( $arcresults ) {
$arc_w_last = '';
$arc_week = '';
if ( $arcresults ) {
foreach ( (array)$arcresults as $arcresult ) {
if ( $arcresult->week != $arc_w_last ) {
$arc_year = $arcresult->yr;
$arc_w_last = $arcresult->week;
$arc_week = get_weekstartend( $arcresult->yyyymmdd, get_option( 'start_of_week' ) );
}
}
}
}
return $arc_week;
}
add_action( 'pre_get_posts', 'weekly_pagination' );
function weekly_pagination( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ( is_home() ) {
$query->set( 'nopaging', true );
add_filter( 'posts_where', 'new_posts_where' );
}
}
}
function new_posts_where( $where ) {
if ( !is_admin() ) {
global $wpdb;
$week = get_this_week();
$start = $end = false;
if ( $week ) {
if ( $week['start'] ) {
$start = date( 'Y-m-d H:i:s', $week['start'] );
}
if ( $week['end'] ) {
$end = date( 'Y-m-d H:i:s', $week['end'] );
}
if ( $start && $end ) {
$where.= " AND $wpdb->posts.post_date >= '$start' AND $wpdb->posts.post_date <= '$end'";
}
} else {
$where.=' AND 1=0';
}
}
return $where;
}