Try this Solution:
- For this you should create custom shortcodes. Please add this code in your theme’s or child theme’s functions.php file:
add_shortcode( 'next_post', 'next_shortcode' );
function next_shortcode($atts) {
global $post;
ob_start();
next_post_link( '<span class="nav-next">%link : Next ></span>', '%title' );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
add_shortcode( 'prev_post', 'prev_shortcode' );
function prev_shortcode($atts) {
global $post;
ob_start();
previous_post_link( '<span class="nav-previous">< Previous : %link</span>', '%title' );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
function my_custom_adjacent_post_where($sql) {
if ( !is_main_query() || !is_singular() || 'your-custom-post-type-slug' != get_post_type() )
return $sql;
$the_post = get_post( get_the_ID() );
$patterns = array();
$patterns[] = '/post_date/';
$patterns[] = '/\'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\"https://wordpress.stackexchange.com/";
$replacements = array();
$replacements[] = 'post_title';
$replacements[] = $the_post->post_title;
return preg_replace( $patterns, $replacements, $sql );
}
add_filter( 'get_next_post_where', 'my_custom_adjacent_post_where' );
add_filter( 'get_previous_post_where', 'my_custom_adjacent_post_where' );
function my_custom_adjacent_post_sort($sql) {
if ( !is_main_query() || !is_singular() || 'your-custom-post-type-slug' != get_post_type() )
return $sql;
$pattern = '/post_date/';
$replacement="post_title";
return preg_replace( $pattern, $replacement, $sql );
}
add_filter( 'get_next_post_sort', 'my_custom_adjacent_post_sort' );
add_filter( 'get_previous_post_sort', 'my_custom_adjacent_post_sort' );
Whereas ‘your-custom-post-type-slug’ is your custom post type slug, that you need to replace in the above code.
- Then, add this shortcode in your Content Template to display Previous and Next post links:
[prev_post] | [next_post]
Expected Output:
< Previous : Post Name | Post Name : Next >