Please add the following code to functions.php:
add_shortcode( 'wpv-post-param', 'wpv_post_param_shortcode' );
function wpv_post_param_shortcode( $atts ) {
if ( !empty( $atts['var'] ) && isset($_GET[$atts['var']]) ) {
$var = (array)$_GET[$atts['var']];
return esc_html( implode( ', ', $var ) );
}
}
add_shortcode( 'toolset_get_custom_pagination_link', 'toolset_get_custom_pagination_link_func');
function toolset_get_custom_pagination_link_func($atts)
{
global $post;
$default = 'wpcf-order-c';
$order = $atts['order'];
$view_ID = 9;
$orderby = $atts['orderby'];
$link = '';
$label = $order=='asc' ? 'Next' : 'Previous';
$args = array('order' => $order, 'orderby_as' => 'numeric');
$args['orderby'] = $orderby ? 'field-' . $orderby : 'field-' . $default;
$posts = get_view_query_results( $view_ID, null, null, $args );
if(isset($posts[0])) {
$i=0;
foreach($posts as $thisPost) {
if ( $post->ID == $thisPost->ID) {
$offset = $i;
break;
}
$i++;
}
$args['offset'] = $offset + 1;
$args['limit'] = 1;
$sortedPosts = get_view_query_results( $view_ID, NULL, NULL, $args );
if(isset($sortedPosts[0])) {
$sortedPost_id = isset($sortedPosts[0]) ? $sortedPosts[0]->ID : -1;
$link = '<a href="' . get_permalink($sortedPost_id);
$link .= $orderby ? '?sortby=' . $orderby : '';
$link .= '">' . $label . '</a>';
$link .= ': <span style="font-size: .8em;">' . get_permalink($sortedPost_id);
$link .= $orderby ? '?sortby=' . $orderby : '';
$link .= '</span>';
}
}
return $link;
}
The first section is a shortcode that will allow us to access URL parameters. The second is another custom shortcode that will help you display your next and previous links using a View. Modify the following values:
'wpcf-order-c' => The slug of your default sort order custom field, appended to 'wpcf-',
9 => The ID of your new View (see below),
$link => You're welcome to modify the format of this link however you would like once it's working appropriately. I'd recommend starting like this.
Create a new View which will be used in your next and previous links to get the correct post URLs. This View should show only the specific post type you want to include, and it should not have any filters. The Loop Output editor should include only the required content. Nothing will be output for this View anyway, we will just use it to get results. Here's a sample Loop:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop></wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
[/wpv-no-items-found]
[wpv-layout-end]
Finally, place the following code in your Content Template for these posts:
[toolset_get_custom_pagination_link order="desc" orderby="[wpv-post-param var='sortby']"]<br />
[toolset_get_custom_pagination_link order="asc" orderby="[wpv-post-param var='sortby']"]
Now you will see a "Previous" link and a "Next" link for your posts, pointing to the appropriate post in sequence.
Finally, test this out by navigating to some posts directly. You should not see '?sortby=' in the URL query string. These posts should be sorted by your custom default sort order field. Next, try adding some sorting parameters to the URL: ?sortby=wpcf-mycustomsortfield
You will notice that once you have applied this sort order, it will be applied to any following post URLs. This way sort order will be respected as long as someone navigates with the next / previous buttons. Let's give this a shot and see how it's working for you, then we can make tweaks as needed.