Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
If you are using menu order for CTP post index and viewing single then you can use this snippets to get the right post link for next/prev.
<?php
function mondira_previous_post_where( $in_same_term, $excluded_terms ) {
global $post, $wpdb;
if ( get_post_type( $post ) == 'portfolio' ) {
return $wpdb->prepare( "WHERE p.menu_order < %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $post->menu_order, $post->post_type);
}
}
add_filter( 'get_previous_post_where', 'mondira_previous_post_where', 10, 2 );
function mondira_next_post_where( $in_same_term, $excluded_terms ) {
global $post, $wpdb;
if ( get_post_type( $post ) == 'portfolio' ) {
return $wpdb->prepare( "WHERE p.menu_order > %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $post->menu_order, $post->post_type);
}
}
add_filter( 'get_next_post_where', 'mondira_next_post_where', 10, 2 );
function mondira_previous_post_sort($order_by) {
if ( get_post_type( $post ) == 'portfolio' ) {
return "ORDER BY p.menu_order desc LIMIT 1";
} else {
return $order_by;
}
}
add_filter( 'get_previous_post_sort', 'mondira_previous_post_sort' );
function mondira_next_post_sort($order_by) {
if ( get_post_type( $post ) == 'portfolio' ) {
return "ORDER BY p.menu_order asc LIMIT 1";
} else {
return $order_by;
}
}
add_filter( 'get_next_post_sort', 'mondira_next_post_sort' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment