I'm just starting to populate my CPT. I want to keep it separate from regular posts. But when I go look at regular posts (in the single.php
template), the Prev/Next nav includes CPT posts.
Interestingly, CPT posts do not appear on the home page list.
UPDATE: I'm using the get_previous_post_join
, get_previous_post_where
, get_previous_post_sort
filters, and their next counterparts. These seem to be affecting all navigation. How do I constrain them to only affect the desired pages?
UPDATE 2: So made code changes as described in my own answer below (encapsulated the get_previous/next_post_* filters in a function, which I called from the CPT single template, before the loop. This is the code in the plugin:
function aaaConsultant_set_nav_filters() {
add_filter('get_previous_post_join', 'aaaConsultant_get_previous_post_join_custom_sort');
add_filter('get_previous_post_where', 'aaaConsultant_get_previous_post_where_custom_sort');
add_filter('get_previous_post_sort', 'aaaConsultant_get_previous_post_sort_custom_sort');
add_filter('get_next_post_join', 'aaaConsultant_get_next_post_join_custom_sort');
add_filter('get_next_post_where', 'aaaConsultant_get_next_post_where_custom_sort');
add_filter('get_next_post_sort', 'aaaConsultant_get_next_post_sort_custom_sort');
}
function aaaConsultant_get_previous_post_join_custom_sort($join) {
global $wpdb;
return "INNER JOIN {$wpdb->prefix}postmeta ON (p.ID = {$wpdb->prefix}postmeta.post_id)";
}
function aaaConsultant_get_previous_post_where_custom_sort($where) {
global $wpdb, $post;
$meta_value = get_post_meta($post->ID, 'aaaConsultant_dateofletter', true);
return "WHERE 1=1 AND p.post_type = 'refletter' AND p.post_status = 'publish' AND {$wpdb->prefix}postmeta.meta_key = 'aaaConsultant_dateofletter' AND {$wpdb->prefix}postmeta.meta_value < '{$meta_value}'";
}
function aaaConsultant_get_previous_post_sort_custom_sort($sort) {
global $wpdb;
return "ORDER BY {$wpdb->prefix}postmeta.meta_value DESC LIMIT 1";
}
function aaaConsultant_get_next_post_join_custom_sort($join) {
global $wpdb;
return "INNER JOIN {$wpdb->prefix}postmeta ON (p.ID = {$wpdb->prefix}postmeta.post_id)";
}
function aaaConsultant_get_next_post_where_custom_sort($where) {
global $wpdb, $post;
$meta_value = get_post_meta($post->ID, 'aaaConsultant_dateofletter', true);
return "WHERE 1=1 AND p.post_type = 'refletter' AND p.post_status = 'publish' AND {$wpdb->prefix}postmeta.meta_key = 'aaaConsultant_dateofletter' AND {$wpdb->prefix}postmeta.meta_value > '{$meta_value}'";
}
function aaaConsultant_get_next_post_sort_custom_sort($sort) {
global $wpdb;
return "ORDER BY {$wpdb->prefix}postmeta.meta_value ASC LIMIT 1";
}
And this is the code in the template:
<?php
aaaConsultant_set_nav_filters();
if (have_posts()) : while (have_posts()) : the_post(); ?>
The problem is that regular posts show up in single.php, but the nav goes back to the beginning of the list of posts, and does not traverse the entire list. Not sure what this is caused by. Will try to deactivate plugin and see what gives.
Also, in a quandary as to whether this should go in a new question, or get added to this one, as it's clearly an effect of the nav changes. (Well, "clearly" is yet to be proven, I suppose.)
UPDATE 3: So, deactivated plugin, and still having issues with nav in regular posts. These were just generated through the Demo Data Creator. But then I made some manual changes to the wp_posts and wp_postmeta tables when I decided to rename the CPT. So these issues may be a result of that messing about.
I'm thinking I'll strip out the generated posts and regenerate.
UPDATE 4: Yes, regenerating the posts seems to have fixed the issue. We'll put it down to my own awkward database manipulations. Not sure whether we want to strip out updates 3 and 4 (and perhaps a bit off Update 2, as well). I'll leave that to the StackExchange sages.