SHOW:
|
|
- or go back to the newest paste.
1 | global $my_exclude_categories, $my_exclude_categories_rss; | |
2 | ||
3 | $my_exclude_categories = array( 1, 3 ); | |
4 | $my_exclude_categories_rss = array( 1, 3 ); | |
5 | ||
6 | function next_week_link( $label = 'Next Week', $display = true ) { | |
7 | if ( $display ) | |
8 | echo get_week_link( $label, 'next' ); | |
9 | else | |
10 | return get_week_link( $label, 'next' ); | |
11 | } | |
12 | ||
13 | ||
14 | function previous_week_link( $label = 'Previous Week', $display = true ) { | |
15 | if ( $display ) | |
16 | echo get_week_link( $label, 'previous' ); | |
17 | else | |
18 | return get_week_link( $label, 'previous' ); | |
19 | } | |
20 | ||
21 | ||
22 | ||
23 | function get_week_link( $label, $next_or_previous = '' ) { | |
24 | ||
25 | $archive_month = get_this_week( $next_or_previous ); | |
26 | ||
27 | if ( $archive_month ) { | |
28 | ||
29 | $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; | |
30 | if ( $next_or_previous == 'next' ) { | |
31 | --$paged; | |
32 | } | |
33 | if ( $next_or_previous == 'previous' ) { | |
34 | ++$paged; | |
35 | } | |
36 | ||
37 | return '<a href="' . get_pagenum_link( (int)$paged ) . '">' . $label . '</a>'; | |
38 | } | |
39 | return ''; | |
40 | ||
41 | } | |
42 | ||
43 | ||
44 | function get_this_week( $next_or_previous = '' ) { | |
45 | ||
46 | $arc_week = array(); | |
47 | $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; | |
48 | ||
49 | if ( $next_or_previous == 'next' && $paged == 1 ) { | |
50 | return $arc_week; | |
51 | } elseif ( $next_or_previous == 'next' && $paged > 1 ) { | |
52 | --$paged; | |
53 | } | |
54 | ||
55 | if ( $next_or_previous == 'previous' ) { | |
56 | ++$paged; | |
57 | } | |
58 | ||
59 | $limit = ( $paged > 1 ) ? ' LIMIT ' . ( $paged - 1 ) . ',1' : " LIMIT 1"; | |
60 | ||
61 | global $wpdb, $my_exclude_categories; | |
62 | $where = get_posts_by_author_sql( 'post' ); | |
63 | //$where = "WHERE post_type = 'post' AND post_status = 'publish'"; | |
64 | ||
65 | $join = ''; | |
66 | $order = 'DESC'; | |
67 | $week = _wp_mysql_week( '`post_date`' ); | |
68 | $tax_query = $tax_query = array( | |
69 | array( | |
70 | 'taxonomy' => 'category', | |
71 | 'terms' => $my_exclude_categories, | |
72 | 'operator' => 'NOT IN' | |
73 | ) | |
74 | ); | |
75 | $tax = get_tax_sql( $tax_query, $wpdb->posts, 'ID' ); | |
76 | $tax_where = $tax['where']; | |
77 | $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 $tax_where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit"; | |
78 | ||
79 | $arcresults = $wpdb->get_results( $query ); | |
80 | ||
81 | if ( $arcresults ) { | |
82 | ||
83 | $arc_w_last = ''; | |
84 | $arc_week = ''; | |
85 | if ( $arcresults ) { | |
86 | foreach ( (array)$arcresults as $arcresult ) { | |
87 | if ( $arcresult->week != $arc_w_last ) { | |
88 | $arc_year = $arcresult->yr; | |
89 | $arc_w_last = $arcresult->week; | |
90 | $arc_week = get_weekstartend( $arcresult->yyyymmdd, get_option( 'start_of_week' ) ); | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | } | |
96 | return $arc_week; | |
97 | } | |
98 | ||
99 | ||
100 | add_action( 'pre_get_posts', 'weekly_pagination' ); | |
101 | function weekly_pagination( $query ) { | |
102 | global $my_exclude_categories, $my_exclude_categories_rss; | |
103 | if ( !is_admin() && $query->is_main_query() ) { | |
104 | if ( is_home() ) { | |
105 | $query->set( 'nopaging', true ); | |
106 | $query->set( 'category__not_in', $my_exclude_categories ); | |
107 | add_filter( 'posts_where', 'new_posts_where' ); | |
108 | } | |
109 | if ( is_feed() ) { | |
110 | $query->set( 'category__not_in', $my_exclude_categories_rss ); | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
115 | ||
116 | function new_posts_where( $where ) { | |
117 | if ( !is_admin() ) { | |
118 | global $wpdb; | |
119 | $week = get_this_week(); | |
120 | ||
121 | $start = $end = false; | |
122 | if ( $week ) { | |
123 | ||
124 | if ( $week['start'] ) { | |
125 | $start = date( 'Y-m-d H:i:s', $week['start'] ); | |
126 | } | |
127 | ||
128 | if ( $week['end'] ) { | |
129 | $end = date( 'Y-m-d H:i:s', $week['end'] ); | |
130 | } | |
131 | ||
132 | if ( $start && $end ) { | |
133 | $where.= " AND $wpdb->posts.post_date >= '$start' AND $wpdb->posts.post_date <= '$end'"; | |
134 | } | |
135 | ||
136 | } else { | |
137 | $where.= ' AND 1=0'; | |
138 | } | |
139 | } | |
140 | return $where; | |
141 | } |