Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Created February 21, 2017 14:55
Show Gist options
  • Save bdeleasa/146e8b0e67af18e0266700e7df9e7490 to your computer and use it in GitHub Desktop.
Save bdeleasa/146e8b0e67af18e0266700e7df9e7490 to your computer and use it in GitHub Desktop.
Orders the Wordpress post navigation links by menu order instead of date.
<?php
add_filter( 'get_next_post_where', 'PLUGINNAME_adjacent_post_where' );
add_filter( 'get_previous_post_where', 'PLUGINNAME_adjacent_post_where' );
/**
* Overrides the post navigation to sort by menu order.
*
* @since 1.0.0
*
* @param $sql
* @return mixed
*/
function PLUGINNAME_adjacent_post_where($sql) {
// Get out now if this isn't the query we want to filter
if ( !is_main_query() || !is_singular() )
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[] = 'menu_order';
$replacements[] = $the_post->menu_order;
return preg_replace( $patterns, $replacements, $sql );
}
add_filter( 'get_next_post_sort', 'PLUGINNAME_adjacent_post_sort' );
add_filter( 'get_previous_post_sort', 'PLUGINNAME_adjacent_post_sort' );
/**
* Overrides the post navigation to sort by menu order.
*
* @since 1.0.0
*
* @param $sql
* @return mixed
*/
function PLUGINNAME_adjacent_post_sort($sql) {
// Get out now if this isn't the query we want to filter
if ( !is_main_query() || !is_singular() )
return $sql;
$pattern = '/post_date/';
$replacement = 'menu_order';
return preg_replace( $pattern, $replacement, $sql );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment