I build a website for a client of mine, that is going to continuously host events for his area.

One of the requirements he has is to expire and remove from the database the events already completed.

The events will belong to categories.

The question is, how I could redirect with 301 the search engines and the visitors to the parent category of each event?

So let's say we have the categories theater and concert.

If I have an even for theater, and the event is expired + removed from the DB, how can I make the redirect to the theater category for all the upcoming visitors?

Any idea?


What if I make a custom table to record the slugs of the events and the category IDs when an event is deleted, and then when I have a 404, to check the DB table for the requested slug and make a redirect using the category ID?

Could that solution be performant given that the site is going to host a few million of events?


Since I have less reputation I can't comment, this is not answered with code but wanted to help, you may check this plugin: https://wordpress.org/plugins/redirect-404-error-page-to-homepage-or-custom-page/ or just take required code from the plugin ( I am not connected with that plugin anyway )

  • Thank you very much for your reply !! :) I will give it a look and I will come back with comments :) ;) – Merianos Nikos Aug 2 '19 at 8:56
  • Unfortunately, this plugin doesn't solve my problem. My case is more general. And I need to have different redirect destination for every single event. So, today I could have 3 categories, but in the next two months I could have 100 categories, and then each deleted event should redirect to the category that initially belonged. – Merianos Nikos Aug 2 '19 at 9:00
  • 1
    There is no record of post once it's deleted so you can not find an attached category with your deleted event. Even if you store event ID and related category ID in a custom table when you create event, there is not any known way ( as far as i know ) to check currently visited post id if post is deleted since there is no record of it. – Den Pat Aug 2 '19 at 12:56
  • 1
    May be you can try use pre_handle_404 hook, this answer may help : wordpress.stackexchange.com/questions/202709/… , try logging wp_query object and see if you can get URL which is throwing 404, if you are able to get this url what you can do is, store this URL when you create event and store category id, now in pre_handle_404, lookup URL in your table and create URL of category from it's ID and redirect to that :) – Den Pat Aug 2 '19 at 12:56

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.