Advertisement
Guest User

Untitled

a guest
Mar 4th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1.  
  2. function next_week_link( $label = 'Next Week' ) {
  3. echo get_week_link( $label, 'next' );
  4. }
  5.  
  6.  
  7. function previous_week_link( $label = 'Previous Week' ) {
  8. echo get_week_link( $label, 'previous' );
  9. }
  10.  
  11.  
  12. function get_week_link( $label, $next_or_previous = '' ) {
  13.  
  14. $archive_month = get_this_week( $next_or_previous );
  15.  
  16. if ( $archive_month ) {
  17.  
  18. $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  19. if ( $next_or_previous == 'next' ) {
  20. --$paged;
  21. }
  22. if ( $next_or_previous == 'previous' ) {
  23. ++$paged;
  24. }
  25.  
  26. return '<a href="' . get_pagenum_link( (int)$paged ) . '">' . $label . '</a>';
  27. }
  28. return '';
  29.  
  30. }
  31.  
  32.  
  33. function get_this_week( $next_or_previous = '' ) {
  34.  
  35. $arc_week = array();
  36. $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  37.  
  38. if ( $next_or_previous == 'next' && $paged == 1 ) {
  39. return $arc_week;
  40. } elseif ( $next_or_previous == 'next' && $paged > 1 ) {
  41. --$paged;
  42. }
  43.  
  44. if ( $next_or_previous == 'previous' ) {
  45. ++$paged;
  46. }
  47.  
  48. $limit = ( $paged > 1 ) ? ' LIMIT ' . ( $paged - 1 ) . ',1' : " LIMIT 1";
  49.  
  50. global $wpdb;
  51. $where = get_posts_by_author_sql( 'post' );
  52. //$where = "WHERE post_type = 'post' AND post_status = 'publish'";
  53.  
  54. $join = '';
  55. $order = 'DESC';
  56. $week = _wp_mysql_week( '`post_date`' );
  57.  
  58. $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";
  59.  
  60. $arcresults = $wpdb->get_results( $query );
  61.  
  62. if ( $arcresults ) {
  63.  
  64. $arc_w_last = '';
  65. $arc_week = '';
  66. if ( $arcresults ) {
  67. foreach ( (array)$arcresults as $arcresult ) {
  68. if ( $arcresult->week != $arc_w_last ) {
  69. $arc_year = $arcresult->yr;
  70. $arc_w_last = $arcresult->week;
  71. $arc_week = get_weekstartend( $arcresult->yyyymmdd, get_option( 'start_of_week' ) );
  72. }
  73. }
  74. }
  75.  
  76. }
  77. return $arc_week;
  78. }
  79.  
  80.  
  81. add_action( 'pre_get_posts', 'weekly_pagination' );
  82. function weekly_pagination( $query ) {
  83. if ( !is_admin() && $query->is_main_query() ) {
  84. if ( is_home() ) {
  85. $query->set( 'nopaging', true );
  86. add_filter( 'posts_where', 'new_posts_where' );
  87. }
  88. }
  89. }
  90.  
  91.  
  92. function new_posts_where( $where ) {
  93. if ( !is_admin() ) {
  94. global $wpdb;
  95. $week = get_this_week();
  96.  
  97. $start = $end = false;
  98. if ( $week ) {
  99.  
  100. if ( $week['start'] ) {
  101. $start = date( 'Y-m-d H:i:s', $week['start'] );
  102. }
  103.  
  104. if ( $week['end'] ) {
  105. $end = date( 'Y-m-d H:i:s', $week['end'] );
  106. }
  107.  
  108. if ( $start && $end ) {
  109. $where.= " AND $wpdb->posts.post_date >= '$start' AND $wpdb->posts.post_date <= '$end'";
  110. }
  111.  
  112. } else {
  113. $where.=' AND 1=0';
  114. }
  115. }
  116. return $where;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement