1

I got a tricky problem I need to solve. So say, you have a custom post type of event, then this event has a scheduled event date by a custom field. And this event is recurring, like every Saturday since the event date until February 3 2019.

And, you want a way so that WP_Query will output those event and sort them by their event date.

Example:

Event A

event_date: Jan 19 2019

recurring_every: Saturday

recur until: Feb 3 2019

That means, when we retrieve post via WP_Query, there would be at least 3 events showing up:

  1. Event A Jan 19.
  2. Event A Jan 26.
  3. Event A Feb 3.

tricky huh? Not sure if this "cloning" and just changing that 'event date'custom field, and order them ASC is possible just by merely WP_Query, but let me know as that will save me alot of time.

So, moving forward, the possible solution I am currently working now is to: clone that post 3 times (for all Saturdays between Jan 19 to February 3). Then update those clone post with the respective event date. In this way, natively, WP_Query would be able to list those post. Then, for each cloned post we add 2 meta keys: _master_post_id and _is_cloned

_master_post_id is to allow us to delegate and redirect user to the actual event post. Like if you will display a cloned eecurring event in frontend, the slug you will use is the slug of the master_post

then, the _is_cloned meta key is to, hopefully, be used so that in the post listing in the admin backend, wp-admin, the cloned posts will not appear, so that editors can't be confused on which event is the 'master' and 'cloned'. (Note that, I'll make it so that updating a master event will also update its cloned events). This way, only the master will show up.

I imagine excluding the cloned posts in the post listing can be solved by pre_get_posts(), (if post has meta key _is_cloned, then exclude) but how can I be assured that all query in the admin backend will behave like this? i.e. exclude on ajax result of post listing of ACF plugin when selecting posts; exclude on post listing when selecting menu items in Appearance -> Menus?

Hope somebody can be consulted with this. or if there's a better approach?

Regards,

  • 1
    You should use a non-hierarchical taxonomy instead of post meta to store your _is_cloned meta key/tag, if you need to tag a post as something, you should use a taxonomy. Filtering out via post meta is obscenely expensive, has a super heavy load on the DB, and is very slow. Querying to a tag named _is_cloned or even better, _is_not_cloned is 1000x faster and makes for an easier to write query. Never exclude, always query for what you actually want ( aka posts that aren't cloned ), and use taxonomies to store things you need to search for later – Tom J Nowell Jul 3 '18 at 13:34

Your Answer

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

Browse other questions tagged or ask your own question.