4

I have a CPT called jobs and a page named Job Apply. I am trying to achieve something like below url:

http://example.com/job-slug/apply

Where, job-slug can be any job. i.e Programmer, Developer etc..

On clicking job's apply button, it should open an apply page which consists a simple form, but url should be as mentioned above.

I am trying to achieve it by using add_rewrite_rule(), but after trying too much, it is still redirecting me on the below url:

http://example.com/apply

Any help would be appreciated. Thanks in advance.

1
  • did you try changing custom post type slug first? Aug 29, 2020 at 15:08

2 Answers 2

1

I assume you have a custom taxonomy for your CPT, so your CPT should be 'job' and your custom taxonomy can then be 'jobs' and then you'd have custom terms within that taxonomy for each job type. The Posts then would consist of the individual job with any associated relevant data (rate of pay, hours, etc).

If that is your setup - and how I would do it - then if you DON'T want the CPT or Taxonomy base in the URL, you do have to do a re-write (see this page for some more helpful advice).

Remove Custom Taxonomy Base

I think the problem you're having is that you're using a Page (Apply) to hold your form and related job data - which is why your re-write isn't working.

Try changing to using a Template that is applied to all posts within the CPT, and put your form (either embedded or with a PHP call to the form) on your Template, using it to also pull in any custom metadata through a loop of your CPT and taxonomy.

1

If you want to keep your current structure I would use a routing library.
With this library you could solve your issue like this:

Routes::map(':job_slug/apply', function($url_params) {
    $params = [];
    $wp_query_args = [
        'post_type' => 'page',
        'posts_per_page' => 1,
        'post_name__in' => ['apply']
    ];

    Routes::load('page.php', $params, $wp_query_args);
});

With $url_params['job_slug'] you are able to access the current job slug with which you could query more information and then pass those via the second parameter of the load method to your template.

The first parameter simply tells it to use the page.php template and the third parameter tells WordPress wich query to execute when reaching this route. In this case it will simply load the Job Apply page.

Your Answer

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

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