0

I'm using the next_post_link and previous_post_link functions in two different scenarios, first in my single-cpt.php and second in my single posts. The name of my cpt is "members".

Note, that I'm using polylang plugin, as I'm in a multilanguage site. So the navigation through posts and members, should be also "taxonomy" restricted (its language).

previous_post_link( '%link', '%title', TRUE, ' ', 'language' );
next_post_link( '%link', '%title', TRUE, ' ', 'language' );

I want to order alphabetically the links for the members case (cpt), and in chronological order for the standard posts case.

My source code is taken from: https://wordpress.stackexchange.com/a/204270/39150

I'm trying this in my functions.php:

add_filter('get_next_post_sort',  'filter_next_and_prev_post_sort');
add_filter('get_previous_post_sort',  'filter_next_and_prev_post_sort');
function filter_next_and_prev_post_sort($sort) {
    global $post;
    $op = ('get_previous_post_sort' == current_filter()) ? 'DESC' : 'ASC';
    //Trying to set the condition for only the members cpt
    if( 'members' == $post->post_type ) {
        $sort = "ORDER BY p.post_title ".$op ." LIMIT 1";
    }
    return $sort;
}

add_filter( 'get_next_post_join', 'navigate_in_same_taxonomy_join', 20);
add_filter( 'get_previous_post_join', 'navigate_in_same_taxonomy_join', 20);
function navigate_in_same_taxonomy_join( ) {
    global $wpdb;
    return  " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
}

add_filter( 'get_next_post_where' , 'filter_next_and_prev_post_where' );
add_filter( 'get_previous_post_where' , 'filter_next_and_prev_post_where' );
function filter_next_and_prev_post_where( $original ) {
    global $wpdb, $post;
    $where = '';
    $taxonomy   = 'language'; //change by the desired taxonomy
    $op = ('get_previous_post_where' == current_filter()) ? '<' : '>';

    if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
    return $original ;
    }

    $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

    $term_array = array_map( 'intval', $term_array );

    if ( ! $term_array || is_wp_error( $term_array ) ) {
    return $original;
    }
    $where = " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
    return $wpdb->prepare( "WHERE p.post_title $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $post->post_title, $post->post_type );
}

The result is that it works perfect for my members cpt, but I get odd results for the standard posts navigation.

Any help will be appreciated! Thanks!

  • 1
    If post filters are functioning in exact same way as members, I suggest to copy-paste the whole code and change the post values to member values. It's very tricky to navigate in this code spaghetti and it could take you days to find your bug. – N00b May 10 '16 at 17:22
  • @N00b you are right, simply removing the filters from functions.php and copying them into "single-members.php" did the trick. Many thanks for your advice! – Capiedge May 10 '16 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.