For a recent client project, I needed a way to redirect to their "Careers" page in case the job ad is currently not published.
The client has a set of roles that they hire for periodically. Their job ads therefore are published when they have a vacancy and set as draft once the position is filled.
In the WordPress hook system, I decided to use the template_redirect
action to check if the queried object is a job ad that is currently set to draft.
That's it, you can adjust the code to your liking. There are other ways to redirect in WordPress, but I decided on template_redirect
because this action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.