How to order adjacent posts (prev / next) by custom field value?

I am using the More Fields and More Types plugins to create a custom post type called product. This custom type is based on the Post type.

One of the fields is named lot which is an integer representing a Lot # for an auction.

Read More

I created a custom category page which generates a list of links for each Lot, ordered by the lot field. This works great.

$args = array(
  'post_type' => array('product'), 
  'cat' => 3,
  'meta_key' => 'lot',
  'orderby' => 'meta_value_num',
  'order' => 'ASC'
);

However… when you click on one of the lots, the Previous & Next post links aren’t ordered by the lot field anymore, they are ordered by publish date (which is the WP default). I am trying to figure out how I can change this default sort order for previous_post_link() and next_post_link() to use custom field lot instead of Publish date.

Related posts

Leave a Reply

1 comment

  1. You can filter SQL to change to your condition in get_adjacent_post() ( source ) :

    $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
    

    Or filter link altogether in adjacent_post_link() ( source ) :

    echo apply_filters( "{$adjacent}_post_link", $format, $link );
    

    PS $adjacent can be next or previous.