2

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

2 Answers 2

1

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

2
  • thanks. I've been trying to find an example of their usage without any luck. Also, I can't figure out how to hook into the get_adjacent_post to check if my SQL is constructed correctly.
    – dashaluna
    Commented Apr 21, 2011 at 16:00
  • 1
    I posted my working filters here: wordpress.stackexchange.com/a/139469/28957
    – cfx
    Commented Mar 28, 2014 at 16:19
0

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
1
  • What was your "some code" in this case? Its a very valid question, but no concrete answer. Commented Aug 1, 2020 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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