This is the code to create the previous and next post links for the Speaker Program demo for my WordCamp Seattle presentation. It focuses on the speaker program developed for the Bellevue Breakfast Rotary Club. Other posts in this series are:
WordCamp Seattle 2014 Speaker Program Dashboard Filters
WordCamp Seattle 2014 Speaker Program Custom Grid and Advanced Search

Speaker Program Custom Prev and Next Links

To get the next and previous to work using the speaker date, we must implement several filters for both the previous and next post links. These filters change the actual query used to process the previous and next links. So, what do these all do?

Well, the custom fields are stored in the post meta table. This is different from the post date, which is in the posts table. So the JOIN filter is used to join the post table and post meta table together. (actually INNER JOIN matching columns in both the Posts and Posts Meta table) The join uses the post ID.

The WHERE filter sets the criteria for the query as does any other WHERE clause. We actually use two of the same fields as the WP_Query shown earlier, and include the meta_key and meta value.

The SORT filter overrides the order by clause allowing us to sort by the meta_value column in ASCENDING sequence for the “NEXT” llink and DESCENDING sequence for the “PREVIOUS” link. Remember that in this case, our meta_value is the speaker_date that we defined with Advanced Custom Fields.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
add_filter('get_previous_post_join', 'rotary_post_join');
add_filter('get_next_post_join', 'rotary_post_join');
add_filter('get_previous_post_where', 'rotary_prev_post_where');
add_filter('get_next_post_where', 'rotary_next_post_where');
add_filter('get_previous_post_sort', 'rotary_prev_post_sort');
add_filter('get_next_post_sort', 'rotary_next_post_sort');
add_filter('next_post_link', 'rotary_filter_next_post_link');
add_filter('previous_post_link', 'rotary_filter_previous_post_link');
 
 
function rotary_post_join($join) {
	global $wpdb;
 
	if ( 'rotary_speakers' == get_post_type() && is_single()) {
		$join = " INNER JOIN $wpdb->postmeta AS pm ON pm.post_id = p.ID";
	}
	return $join;
 
}
function rotary_prev_post_where($where) {
	global $wpdb, $post;
	$speakerDate = get_post_meta($post->ID, 'speaker_date', true);
 
	if ( 'rotary_speakers' == get_post_type() && is_single()) {
		$where = $wpdb->prepare(" WHERE pm.meta_key = %s AND pm.meta_value < '$speakerDate' AND p.post_type = %s AND p.post_status = 'publish'", 'speaker_date', 'rotary_speakers');
	}
 
	return $where;
 
 
}
function rotary_next_post_where($where) {
	global $wpdb, $post;
	$speakerDate = get_post_meta($post->ID, 'speaker_date', true);
 
	if ( 'rotary_speakers' == get_post_type() && is_single()) {
		$where = $wpdb->prepare(" WHERE pm.meta_key = %s AND pm.meta_value > '$speakerDate' AND p.post_type = %s AND p.post_status = 'publish'", 'speaker_date', 'rotary_speakers');
	}
	return $where;
 
 
}
 
function rotary_prev_post_sort($order) {	
 
	if ( 'rotary_speakers' == get_post_type() && is_single()) {	
		$order = " ORDER BY pm.meta_value DESC LIMIT 1";
	}
 
	return $order;
}
function rotary_next_post_sort($order) {	
	if ( 'rotary_speakers' == get_post_type() && is_single()) {	
		$order = " ORDER BY pm.meta_value ASC LIMIT 1";
	}
	return $order;
}
function rotary_filter_next_post_link($link) {
	if ( 'rotary_speakers' == get_post_type() && is_single()) {
		$next_post = get_next_post();
		if( is_object( $next_post ) ) {
			$speakerDate = get_post_meta($next_post->ID, 'speaker_date', true);
			$link = preg_replace('/<a(.+?)>.+?<\/a>/i',"<a$1><span>".date('l ', strtotime($speakerDate))."</span>".date('M dS, Y', strtotime($speakerDate))." &gt;</a>",$link);
		}	
	}
	return $link;
}
 
function rotary_filter_previous_post_link($link) {
	if ( 'rotary_speakers' == get_post_type() && is_single()) {
		$previous_post = get_previous_post();
		if( is_object( $previous_post ) ) {
			$speakerDate = get_post_meta($previous_post->ID, 'speaker_date', true);
			$link = preg_replace('/<a(.+?)>.+?<\/a>/i',"<a$1>&lt; <span>".date('l ', strtotime($speakerDate))."</span>".date('M dS, Y', strtotime($speakerDate))."</a>",$link);
		}	
	}
	return $link;
 
}