I want to get the ID of adjacent posts.

For posts sorted chronologically you can use:


Are there any standard methods for finding the adjacent post if the posts are sorted by custom field value. Unless its very simple my skills are not up to writing a new function for this purpose.


  • 1
    Do you want to get the ID or a next/previous post link? For latter i can recommend a plugin, especially if you are not up to the task of coding it yourself: wordpress.org/plugins/ambrosite-nextprevious-post-link-plus ambrosite.com/plugins/… The former, just getting the ID, maybe isn't part of the functionality the plugin offers you, but a look at the code gives you a hint what way to go. – Nicolai May 29 '13 at 19:31
  • @ngsonst Thanks. I'm using that plugin to get the links and that's a good thought of yours, I'll go through the code and see if I can find a solution. – dorich May 30 '13 at 6:54
  • If you can't get it working come back and ping me by putting @s_ha_dum in a comment. I have code that will reorder alphabetically by title. It should be adaptable for use with a meta field. – s_ha_dum May 30 '13 at 15:02

Hmmmm, the solution to this problem was to read the details of the plugin. It has the capability of returning values other than a link to the next post - this includes the ID of the Next and Previous Post.

In case anyone else reads this in the future and wants to see how to get the ID all you have to do is something like:

<?php $link=next_post_link_plus( array('order_by' => 'custom', 'meta_key' => 'Sort Number','return'=>'id') );?>

Then you can recover any information that you need from the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.