0

I'm exhaustively searching for a method to provide Next and Previous Post Links in a different way from which it usually appears in Single Post.

By DEFAULT it:

  • Is chronological ordered
  • Links to posts from all blog categories

But I NEED it:

  • Ordered by an "order" variable (provided by woocommerce)
  • Linking to posts on SAME CATEGORY only

I'm not a developer: the code below will arrange the posts in alphabetical order. Maybe with some tweaking is possible to make the code arrange the posts by menu_order and not post_title?

enter image description here

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) {
    $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_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   = 'category';
  $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 );
}

I appreciate the help!

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.