7

I have a custom post type called Event in which I want to add a location chosen from a large list of locations. To make this process easier I want to filter these locations by a category called locality.

To achieve this I have created a custom post type called Locations and a custom taxonomy called Locality. A field group holding two dropdowns one for Locality (Taxonomy) and another for Locations (Post Object) has been added to each event.

What I haven't managed to achieve is the filter of locations by the locality chosen.

Steve Cortis
  • 522
  • 1
  • 6
  • 25
  • Can you show your code ? – Ranjit Mar 08 '17 at 12:37
  • No code was created for the above, it was all created through ACF's UI – Steve Cortis Mar 08 '17 at 12:38
  • It is confusing. First you say " Location is a post type and Locality is Taxonomy". Then you said "A field group holding two dropdowns one for Locality (Post Object) and another for Locations (Taxonomy) has been added to each event." – Ranjit Mar 08 '17 at 12:43
  • Sorry, my bad fixed it to Locality (Taxonomy) Locations (Post Object) – Steve Cortis Mar 08 '17 at 12:44
  • How can you assign a custom post type with another custom type. Means Event is already a custom post type. You have added another custom post type Locations. What is the relation between them. Are you get my point ? – Ranjit Mar 08 '17 at 12:55
  • The relation is that each Location has a locality assigned to it. So for example Location (Museum of London) is assigned a locality (London) in the form of a taxonomy. – Steve Cortis Mar 08 '17 at 13:57
  • You will need to code that yourself. Here is a discussion on a similar situation. This is to show fields, but I am sure you could trigger to reload data also. If not, you can always reload the same field. https://support.advancedcustomfields.com/forums/topic/load-dynamically-fields-on-select-change/ – ibenic Mar 15 '17 at 11:55
  • You can use the field type Relationship to link you stuff together - here you can search in post types and taxonomies – Stender Mar 17 '17 at 09:17

4 Answers4

0

I don't believe you can dynamically filter the choices of one custom field based on the current choice of another, and change that choice on the adminend. You can filter the choices that show up in your fields with a query modification that's very similar to WP_Query, and I'm pretty sure that can't be done on the fly.

I would almost cheat and instead of making posts with taxonomy assigned to them, include both the locality and the location in the post name itself.

So say, post [Museum of London] with taxonomy [London]

would become

[London][Museum of London]

[London][Hyde Park]... etc

or something similar. I'm presuming you only want to assign one location to any specific locality...

Then I'd create a relationship field and whenever I'm looking for Museum of London I'd type

London Muse...

Joris G
  • 66
  • 1
  • 8
0

It looks like this question is stale by now, but you would make your life easier if you added two custom fields to Event - location and locality. That way you don't have to join two tables and can easily use WP meta_query to filter posts.

0

What i understood is that you want that whenever you select the locality the location for it should be shown there and at last you can filter Events with locations.

=> To set location you assigned two drop-down for locality and location. You should use ajax. when i select any locality than the next drop-down has the options for the selected locality.

=> To add value in event post: you might have two options i) you may add location as taxonomy and search using "tax query" to filter ii) add location as parent of event and search "child of" in wp_query to filter

0

You have two options:

  1. Program custom js to filter the locations field onchange of the localities field. This would not be so hard to do, because ACF sets the field id's for every field in their html. You can enqueue this js script in an admin hook (probably admin_enqueue_scripts, check this solution) and filter for the custom post type.

  2. It can all be done in the ACF gui, but it's a bit of a trick, also because of the fact that the conditional logic for acf's taxonomy field is lacking. What you have to do is create as many Location fields in the Event acf fields as you have localities. Then set these locations to specific localities. enter image description here Now comes the weird part. You would want to set the conditional logic to display if the locality is e.g. Russia, but it lacks a 'specific value' field. So you have to set two rules, one to set the 'greater than' and one to set the 'lesser than' for the id of the locality. enter image description here. This will be a pain if you have a lot of localities, but it might be a good option if your localities list has no more than a few items. If you name the location fields all 'location' than you can just get the value with get_field('location'). I tested this and there is no conflict in naming all the location fields location. ACF has done a pretty decent job there, having unique names for these fields also to be able to name these fields as you whish.

Jos
  • 1,387
  • 1
  • 13
  • 27