I've scoured for QUITE a while, am looking for method to paginate on a single page template, orderby title (client likes first name sorting of team members)
I assume this would include a way to offset the pagination by title or ?.
The following will paginate just fine by post order, not by title.
<div class="paginate">
<div class="paginate_left">
<?php if( get_previous_post() ) : ?>
<?php previous_post_link('%link ', '<i class="fas fa-caret-square-left"></i>') ?><?php previous_post_link('%link') ?>
<?php endif; ?>
</div>
<div class="paginate_right">
<?php if( get_next_post() ) : ?>
<?php next_post_link('%link') ?><?php next_post_link('%link ', ' <i class="fas fa-caret-square-right"></i>') ?>
<?php endif; ?>
</div>
How do you manually paginate/query against a custom post type, orderby => title, offset by the title of the post that's populating the single page template? Thank you for any help.
I've scoured for QUITE a while, am looking for method to paginate on a single page template, orderby title (client likes first name sorting of team members)
I assume this would include a way to offset the pagination by title or ?.
The following will paginate just fine by post order, not by title.
<div class="paginate">
<div class="paginate_left">
<?php if( get_previous_post() ) : ?>
<?php previous_post_link('%link ', '<i class="fas fa-caret-square-left"></i>') ?><?php previous_post_link('%link') ?>
<?php endif; ?>
</div>
<div class="paginate_right">
<?php if( get_next_post() ) : ?>
<?php next_post_link('%link') ?><?php next_post_link('%link ', ' <i class="fas fa-caret-square-right"></i>') ?>
<?php endif; ?>
</div>
How do you manually paginate/query against a custom post type, orderby => title, offset by the title of the post that's populating the single page template? Thank you for any help.
Share Improve this question edited Sep 11, 2019 at 14:17 user3460707 asked Sep 10, 2019 at 21:58 user3460707user3460707 112 bronze badges1 Answer
Reset to default 0Try 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}\'/';
$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 >