Next/Previous Links: Alphabetically and from Same Taxonomy for my CPT Only

I’m looking for a way to set Next/Previous links alphabetically and from same taxonomy for my custom post types. So I’ve found this questions: https://wordpress.stackexchange.com/a/204270/182104

It’s a bit old, but it’s still working! The only thing missing that I need is a way to apply the sort ONLY to my Custom Post Type.

Can someone please help me set Next/Previous Links alphabetically and from same taxonomy apply only on my CPT?

This is my actual code, taken from https://wordpress.stackexchange.com/a/204270/182104:

// PREV + NEXT: ORDER
function filter_next_and_prev_post_sort($sort) {
$op = (‘get_previous_post_sort’ == current_filter()) ? ‘DESC’ : ‘ASC’;
$sort = “ORDER BY p.post_title “.$op .” LIMIT 1″;
return $sort;
}
add_filter(‘get_next_post_sort’, ‘filter_next_and_prev_post_sort’);
add_filter(‘get_previous_post_sort’, ‘filter_next_and_prev_post_sort’);

// PREV + NEXT: JOIN
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_join’, ‘navigate_in_same_taxonomy_join’, 20);
add_filter( ‘get_previous_post_join’, ‘navigate_in_same_taxonomy_join’, 20 );

// PREV + NEXT: WHERE IN CUSTOM TAXONOMY “collezione”
function filter_next_and_prev_post_where( $original ) {
global $wpdb, $post;
$where = ”;
$taxonomy = ‘collezione’;
$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 );
}
add_filter( ‘get_next_post_where’ , ‘filter_next_and_prev_post_where’ );
add_filter( ‘get_previous_post_where’ , ‘filter_next_and_prev_post_where’ );

Read more here:: Next/Previous Links: Alphabetically and from Same Taxonomy for my CPT Only

Leave a Reply

Your email address will not be published. Required fields are marked *