Skip to content

Instantly share code, notes, and snippets.

@juyal-ahmed
Last active August 29, 2015 14:08
Show Gist options
  • Save juyal-ahmed/59eb3fc5d76d6539ee59 to your computer and use it in GitHub Desktop.
Save juyal-ahmed/59eb3fc5d76d6539ee59 to your computer and use it in GitHub Desktop.
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