specify meta_key / meta_value condition for prev_post_link and next_post_link

I’ve got a custom post type (CPT) called event. Every event has got an associated meta_key called event_date.

I want to make sure that events with empty event_date won’t appear in my list of all events and in the prev/next event navigation when viewing a single event. I also want to order events by the event_date meta_key.

I managed to exclude events with empty event_date and order them correctly in the overall list of events by hooking into pre_get_posts hook.

Is it possible to hook into prev_post_link and next_post_link functions to specify meta_key select condition and meta_key order condition without the plugin? Is there a hook for it?

[P.S. note]:
I also tried using Ambrosite Next/Previous Post Link Plus plugin. I figured out how to order events by meta_key. However, the events with empty event_date meta_value are still appearing in the prev/next navigation when viewing a single event.

It doesn’t seem to be possible to hook into WP before the plugin, because I think it constructs its own SQL. I could use ex_posts with the list of all the event ids to exclude, but that would mean extra query to the db.

Many thanks,
Dasha

Solutions Collecting From Web of "specify meta_key / meta_value condition for prev_post_link and next_post_link"

prev and next_post_link ultimately call the get_adjacent_post function to query the db, which has filterable $join $where and $sort vars.

I ended up using the Ambrosite Next/Previous Post Link Plus plugin.

As to an empty event_date meta key – I have added some code to prevent a post from being saved if meta key value isn’t correct.

Have a look at this posts:

  1. Modifying WordPress post status on Publish
  2. don’t publish custom post type post if a meta data field isn’t valid