1

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.

2 Answers 2

2

The issue was that the get_previous/next nav filters were "out in the open" in the plugin; so they were executed every time the plugin was loaded. I put them in a function called myplugin_set_nav_filters(), which I invoked before the loop in the CPT single template, and everything worked as advertised.

Now the nav on regular posts is no longer being filtered by the CPT posts; and the CPT page is getting the filters it needs.

I hope this helps someone.

3
  • Please do not forget to accept your own answer to show others that this problem is solved.
    – Ralf912
    Apr 28, 2013 at 8:23
  • 1
    This answer would be more useful to future visitors if you included the code.
    – s_ha_dum
    Apr 28, 2013 at 13:08
  • @s_ha_dum: will do. StackExchange won't let me accept my own answer for 2 days. I'll add my code to the answer. But right now, there seems to still be a problem with the nav for regular posts. Will update question. Apr 28, 2013 at 14:46
0

The main reason why it included everything is because your custom sort code was set to work for everything. It wasn't filtered at all so in the where function where you put AND p.post_type = 'refletter' it added the post type refletter to every previous and next link.

What you should do is add a filter to each of the functions that lets them only work when you are in a post of that post type:

function aaaConsultant_get_previous_post_join_custom_sort($join) {
        global $wpdb, $post;  //globalize $post
        if( $post->post_type !== 'refletter' ) return //returns if the custom post type isn't correct
        return "INNER JOIN {$wpdb->prefix}postmeta ON (p.ID = {$wpdb->prefix}postmeta.post_id)";
    }

Doing that in all your functions will solve your problem

1
  • Thanks @MannyFleurmond. I do prefer the other solution because it only invokes the filter when it's needed. This one would also work, but it would be invoked every time. Apr 28, 2013 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.