Skip Navigation

[Resolved] Advice on setting up location custom field on Jobs CPT

This thread is resolved. Here is a description of the problem and solution.

Problem:

The customer had some very specific requirements for storing multi-level location information with job listings and asked for the best approach.

Solution:

Shared a couple of possible approaches, along with their benefits and drawbacks.

Relevant Documentation:

n/a

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#2251039

I have a jobs CPT and need advice on how to configure the location field for my scenario.

Scenario
1. Location has 4 attributes - country, state, city, Remote flag (Y/N)
2. Country is required in all posts
3. Remote OR State-City Combo is required per location. If the user selects one, the other should be greyed out or hidden.
4. A job can have multiple locations.
5. State is only relevant if the country is the US or CA

An example of locations for a job
US -- San Francisco -- CA
US -- Seattle -- WA
US -- Remote
CA -- Vancouver -- BC
CA -- Remote
UK -- London
UK -- Remote

I tried to use repeatable field groups for this but was unable to put conditions on state visibility based on country value. I was unable to find a way to disallow entry in city field if remote is checked.

Can you please guide me how to set this up with Toolset?

#2251389

Hi,

Thank you for contacting us and I'd be happy to assist.

> 4. A job can have multiple locations.

- Even if you'll manage to store multiple locations with an individual job post, using some creative/technical workaround, this will affect your ability to perform a search using those locations, in a post view for jobs.

For this reason, my recommendation would be to create an individual duplicated job post/opening, for each location.

To store actual location information with each job, you can use a hierarchal taxonomy "Location", since, taxonomies are optimized for the search.

The structure of the terms in this taxonomy would like this:

- US
-- CA
--- San Francisco
-- WA
--- Seattle
- Canada
-- BC
--- Vancouver
- UK
-- London
-- Birmingham

Note, how for countries where a states matters, the second level is the state and the third level is the city, while for others the city is at the second level (without any state).

For marking a job as remote, you can include a custom field "Remote" with "yes" and "no".

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2251837

Waqar,

Thanks for trying to solve this.

The taxonomy approach creates a few issues
1. There is no way for me to track all cities in the US or even Canada. So I am not sure how will I create a dropdown for people to select cities. It can get very messy very quickly. Imagine someone new clicks on the dropdown and they see 100s of cities. It is overwhelming, especially when CRED does now allow search in a select field. That is a bad experience for the user.

2. Can fields in repeatable-fields-group be made searchable? For example, if I have a repeatable field group for state and city where the state is a drop-down and city is a text field, can I search job posts just by city or just by the state?

I am comfortable with asking the employer to post a new job for each country. But for one country, I would like to give the author an opportunity to add multiple locations with a state and city combo. Here, the state can come from a drop-down but users should be allowed to enter a city on their own. Can I accomplish this with Toolset?

#2253971

Thanks for writing back.

1. Your concerns about the user experience in the all-inclusive taxonomy approach are valid and make sense.

2. The fields in the repeatable field groups are searchable in the view that shows the repeatable field group posts, but not inside the view that shows the parent posts that hold the repeatable field group.

For example, suppose you have a post type "Jobs" which has a repeatable field group "Job Locations". If you'll create a view for "Jobs", then you'll only be able to use the custom fields directly attached to the "Jobs" posts for the search/filtering. You'll have to create a separate independent view that shows the "Job Locations" posts, in which the custom fields from the repeatable field group can be used for the search/filtering.

Because the entries in the repeatable field groups are saved as a separate post type, custom fields for the "Jobs" can't be used for search/filtering of view that shows "Job Locations" and likewise, custom fields for the "Job Locations", can't be used for the search/filtering of view that shows "Jobs".

A possible workaround, in this case, can be, that you can offer two different search forms to the visitors. First for searching the job posts, based on the fields attached to this post type. And the second one for the search by location, which will be a view that will be showing the "Job Locations" posts, with the location fields which are part of the repeatable field group.

3. Based on what we've discussed in this thread, the possible approach that will still involve a fair amount of customization, would be to use the combination of custom post types, post relationships, and custom taxonomies for storing multiple location information with the job listings.

Custom Post Types:
a). Country
b). State
c). City

Relationships:
a). One-to-many between Country and State
b). One-to-many between State and City

Note: these custom post types and relationships will only be needed for the establishing the connection between the location attributes. For storing the actual location information with the job listings, flat non-hierarchal custom taxonomies attached to the job listings post type will be used.

a). Country
b). State
c). City

Here is how the process of the job posting will look like:

a). A user will fill the add a new job posting form and for country selection there will be a select type generic field. The options of this field will be populated using a custom shortcode that gets the list of countries from the "Country" post type.

On form submission, the "cred_save_data" hook can be used to set the selected "Country" as a selected term in the "Country" taxonomy for the current job.

b). After saving the first form, the visitor will see a button to add the location information to this newly created job post. On clicking, he/she will be redirected to the second form which will be a form to edit the newly entered job post, that will have one select type generic field to select a "State". The options of this generic field can be populated using a custom shortcode that will get the list of all the "States" posts, which are related to the job's selected country taxonomy term.

You can add a single line generic field, which can be used to enter a new "State" name, if the desired state is not already available in the list.

On form submission, the "cred_save_data" hook can be used to set the selected "State" as a selected term in the "State" taxonomy for the current job.

If the user has entered a new "State" field, then be sure to include its post in the "State" post type and also connect it to the current job post's country post.

c). On submission of the "State" selection form, the visitor can be redirected to another form to edit the same job post, which will also include one select type generic field to select a city. The options of this generic field can be populated using a custom shortcode that will get the list of all the "City" posts, which are related to the job's selected state taxonomy term.

You can add a single line generic field, which can be used to enter a new "City" name, if the desired city is not already available in the list.

On form submission, the "cred_save_data" hook can be used to set the selected "City" as a selected term in the "City" taxonomy for the current job.

If the user has entered a new "City" field, then be sure to include its post in the "City" post type and also connect it to the current job post's state post.

d). To add another location with the same job post, the visitor can use the add location button from step (b) again, to first select a another state and then another city.

I hope this helps and let me know if you have any follow-up questions.

#2254439

Waqar,

I appreciate the insight and in theory, this solution makes sense. Having said that, I can't image a user jumping through 3 forms to enter a location field. It is just too much friction.

A lot of platforms find a way to get location information without making a user go through multiple hoops. Is there a way to update the entries via AJAX and still stay on the same form?
For example, when you user selects a country field, an AJAX request assigns taxonomy to the post or creates a new country post, if none exists.

Then when the user goes to the state field, the newly created country is already available on the same form. All this needs to happen without reloading the form (or at least making it appear to the user that we are on the same form).

#2255263

The form or page reloading is necessary in this case because, in our custom shortcodes, we'll need to know which location item was selected in the previous step, so that only the relevant options can be presented in the next.

Technically speaking the all-in-one fully AJAX-based form can be implemented, with complex customization and will require a good understanding of how jQuery, AJAX, WordPress, and PHP works.

In your form to add a new job post, you can include the generic fields for each level of location information and then dynamically populate their options using AJAX calls, whenever the selection in the respective parent input field changes.

Once the generic fields have the correct location items selected and the form is submitted, you can process that data to store it any way you want, using the custom PHP function attached to the "cred_save_data" hook.

Here are some useful guides on using AJAX calls in the WordPress environment:
https://codex.wordpress.org/AJAX_in_Plugins
hidden link
hidden link

For more personalized assistance around customization, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2256905

My issue is resolved now. Thank you!