[WordPress] How to display posts from the same category using next/previous post link

In WordPress, the post navigation enables users to move to the previous or next post in an individual post. The previous_post_link and next_post_link functions are used to move to the previous or next posts. If you want to restrict the post navigation to the same category, you can use the following code:

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' , 'navigate_in_same_taxonomy_where' );
add_filter( 'get_previous_post_where' , 'navigate_in_same_taxonomy_where' );
function navigate_in_same_taxonomy_where( $original ) {
global $wpdb, $post;
$where = '';
$taxonomy = 'category';
$op = ('get_previous_post_where' == current_filter()) ? '<' : '>';
$where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
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_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $post->post_date, $post->post_type );
}
// Source: presscustomizr.com

Please copy and paste the above code to your theme's function file (functions.php). It's desirable to create the child theme.

Leave a Comment

프리미엄 테마 그래픽 동영상 템플릿 무료 다운로드