0

I've registered a custom post type as follows:-

/**
 *  Register Add Job
 *
 */

add_action( 'init', 'rv_jobs_cpt' );
function rv_jobs_cpt() {

    $labels = array(
        'name'               => _x( 'Jobs', 'post type general name', 'engwp' ),
        'singular_name'      => _x( 'Jobs', 'post type singular name', 'engwp' ),
        'menu_name'          => _x( 'Jobs', 'admin menu', 'engwp' ),
        'name_admin_bar'     => _x( 'Jobs', 'add new on admin bar', 'engwp' ),
        'add_new'            => _x( 'Add New Job', 'Property', 'engwp' ),
        'add_new_item'       => __( 'Add New Job', 'engwp' ),
        'new_item'           => __( 'New Job', 'engwp' ),
        'edit_item'          => __( 'Edit Job', 'engwp' ),
        'view_item'          => __( 'View Jobs', 'engwp' ),
        'all_items'          => __( 'All Jobs', 'engwp' ),
        'search_items'       => __( 'Search Jobs', 'engwp' ),
        'parent_item_colon'  => __( 'Parent Jobs:', 'engwp' ),
        'not_found'          => __( 'No Jobs found.', 'engwp' ),
        'not_found_in_trash' => __( 'No Jobs found in Trash.', 'engwp' )
    );

    $args = array(
        'description'         => __( 'Jobs', 'engwp' ),
        'labels'          => $labels,
        'supports'        => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
        'taxonomies'        => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'            => true,
        'publicly_queryable'    => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'jobs' ), 
        'show_ui'           => true,
        'menu_icon'         => 'dashicons-universal-access-alt',
        'show_in_menu'          => true,
        'show_in_nav_menus' => true,
        'show_in_admin_bar' => true,
         'menu_position'    => 31,
        'can_export'        => true,
        'has_archive'       => true,
        'exclude_from_search'   => false,
        'capability_type'   => 'post',
    );

    register_post_type( 'jobs', $args );

}

I am then using ACF Pro to populate the custom fields for this post type. Two of the custom fields are Job Sector and Job Location

I have a job search facility which uses a form on the homepage which redirects to /jobs/ (archive-jobs.php) using the POST method. I am then using WP_Query to display the jobs based on the search parameters.

Although this is working, I now need to change the URL based on their search. For example if someone was to search for Finance jobs the URL would need to be: /jobs/finance, or /jobs/marketing if they were to search for Marketings jobs.

If they were to select 'Finance' as the job type, and also 'Nottingham' as the location, the URL would then need to become /jobs/finance/in-nottingham

Is this possible to do? As these pages would just go to a 404 page if you were to access them at the moment, and it wouldn't make sense to create an archive page for each of the different types of variations.

Any help would be much appreciated, thanks in advance!

EDIT:

A complete structure would be as follows:-

/jobs/finance
/jobs/hr
/jobs/procurement
/jobs/marketing
/jobs/office-support

/jobs/finance/in-nottingham
/jobs/finance/in-leicester
/jobs/finance/in-derby
/jobs/finance/in-lincoln

/jobs/hr/in-nottingham
/jobs/hr/in-leicester
/jobs/hr/in-derby
/jobs/hr/in-lincoln

/jobs/procurement/in-nottingham
/jobs/procurement/in-leicester
/jobs/procurement/in-derby
/jobs/procurement/in-lincoln

/jobs/marketing/in-nottingham
/jobs/marketing/in-leicester
/jobs/marketing/in-derby
/jobs/marketing/in-lincoln

/jobs/office-support/in-nottingham
/jobs/office-support/in-leicester
/jobs/office-support/in-derby
/jobs/office-support/in-lincoln

/jobs/leicester
/jobs/nottingham
/jobs/derby
/jobs/lincoln
/jobs/other
/jobs/international
0

First what you are making doesn't quite sound like a search to me. I think the more apt term for such kind of filtering is faceting.

Second, you probably are firmly in the need to write custom rewrite rules for this, Rewrite API and everything.

I think the main hold up is that your structure isn't completely unambiguous. If I follow it right you are planning for:

  • /jobs/[job]
  • /jobs/[sector]/
  • /jobs/[sector]/in-[location]/
  • and I would guess few more, like just location?

The issue is WP cannot outright guess if arbitrary string in URL is a job or sector. It relies on structure of the rule, that's why those native URLs are like /category/[category]/, explicit level makes it clear which taxonomy it is.

So your next steps are:

  1. Write out complete structure and work on it until it's completely unambiguous.
  2. Use Rewrite API to implement each rule you end up with.
  • Thanks @Rarst - that's exactly right, I've added a complete list of structure, issue I see is that [sector] & [location] are acf custom fields in the jobs custom post type, so even by adding rewrites I sitll don't know how this is going to work? – nsilva May 31 '17 at 10:41
  • Some time with Rewrite API docs is in order, it's not quite my area so unfortunately I wouldn't be able to write out ready-made example for you on top of my head. In a nutshell you need to create rules which will turn URL into query variables, then use query variables to construct/modify a query for the page. – Rarst May 31 '17 at 10:47

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.