I have a series of posts that are ordered by a meta_key value. They could also be arranged by menu order, if necessary.
The next/prev post links (generated by next_post_link
, previous_post_link
, or posts_nav_link
all navigate by chronology. While I understand this default behaviour, I don’t understand how to change it. I found that it maps through to adjacent_post_link in link-template.php, but then it starts to seem fairly hard-coded. Is it recommended to re-write this from scratch to replace it, or is there a better solution.
Understanding the internals
The “sort” order of adjacent (next/prev) posts is not really a sort “order”. It’s a separate query on each request/page, but it sorts the query by the
post_date
– or the post parent if you have a hierarchical post as currently displayed object.When you take a look at the internals of
next_post_link()
, then you see that it’s basically an API wrapper foradjacent_post_link()
. The later function callsget_adjacent_post()
internally with the$previous
argument/flag set tobool(true|false)
to grab the next or previous post link.What to filter?
After digging deeper into it, you’ll see that
get_adjacent_post()
Source link has some nice filters for its output (a.k.a. query result): (Filter Name/Arguments)"get_{$adjacent}_post_join"
"get_{$adjacent}_post_where"
"get_{$adjacent}_post_sort"
So you can do alot with it. That starts with filtering the
WHERE
clause, as well as theJOIN
ed table and theORDER BY
statement.The result gets cached in memory for the current request, so it doesn’t add additional queries if you call that function multiple times on a single page.
Automatic query building
As @StephenHarris pointed out in the comments, there’s a core function that might come in handy when building the SQL Query:
get_meta_sql()
– Examples in Codex. Basically this function is just used to build the meta SQL statement that gets used inWP_Query
, but you can use it in this case (or others) as well. The argument that you throw into it is an array, the exact same that would add to aWP_Query
.The return value is an array:
So you can use
$sql['join']
and$sql['where']
in your callback.Dependencies to keep in mind
In your case the easiest thing would be to intercept it in a small (mu)plugin or in your themes functions.php file and alter it depending on the
$adjacent = $previous ? 'previous' : 'next';
variable and the$order = $previous ? 'DESC' : 'ASC';
variable:The actual filter names
So the filter names are:
get_previous_post_join
,get_next_post_join
get_previous_post_where
,get_next_post_where
get_previous_post_sort
,get_next_post_sort
Wrapped up as a plugin
…and the filter callback would be (for example) something like the following:
Kaiser’s answer is awesome and thorough, however just changing the ORDER BY clause isn’t enough unless your
menu_order
matches your chronological order.I can’t take credit for this, but I found the following code in this gist:
I’ve modified the function names for WP.SE.
If you only change the ORDER BY clause, the query still looks for posts greater than or less than the current post date. If your posts aren’t in chronological order, you won’t get the right post.
This changes the where clause to look for posts where the menu_order is greater than or less than the current post’s menu_order, in addition to modifying the orderby clause.
The orderby clause also shouldn’t be hardcoded to use DESC as it will need to switch based on whether you are getting the next or previous post link.
Tried to hook in without success.
Might be just a problem of my configuration, but for those who can’t make the hook work, here is the simplest solution:
Based on @Szabolcs Páll’s answer I’ve created this utility class with helper methods to be able to get posts of type by menu order and get the next and previous post by menu order as well. I’ve additionally added conditions to check if the current post is the first or last post to get the last or first post respectively.
For example:
The full class:
FWIW hereâs how you can order by
menu_order
for a specific custom post type:Hope this helps someone else!
Based on @Szabolcs Páll’s answer and bbloomer’s post on adding next/prev buttons in WooCommerce Single Product Page, I created this code.
It sorts all products by meta key and adding prev/next buttons above + below the product.
(The meta key can be an ACF field too!)
If you want the extra scss file I used:
_prev-next-buttons.scss
I find this small plugin really handy: http://wordpress.org/plugins/wp-query-powered-adjacent-post-link/
This worked for me:
Taken from:
https://stackoverflow.com/questions/16495117/how-to-skip-certain-links-on-adjacent-posts-in-wordpress
I had issues with this too. I magically got it to work like this:
https://wordpress.org/plugins/simple-custom-post-order/
Besides easily ordering with drag and drop, This plugin makes sure prev and next work by menu order. Unfortunately it might work in the wrong order with DSC instead of ASC. To fix this we can create a reverse post navigation function.
And i didn’t have to write a word of code myself 🙂
I edited Szabolcs Páll’s code above to order by a custom meta_key and within a specific category but also to try to add conditionals in for the first and last posts.
On first and last post it was not showing the correct next/prev link with the original code, only showing a link for the current post id I was on.
This worked for me below but not sure if there are any potential issues with it. (I’m not the most advanced coder)
?>
I have found a much easier way to achieve a meta-key based post navigation, without the need to modify functions.php.
My example: You have a products.php and you want to switch between products. The previous product is the next cheaper one, the next product the next more expensive one.
Here comes my solution for single.php: