Home Forums General Issues Conditional nested relations?


Conditional nested relations?

  • I’ve generated a huge list of car brands and corresponding models manually. I’ve used the post id of the brand as the parent id of the models to make the model a child of the parent, like this (dummy data):

    (22, 1, ‘<date>’, ‘<date>’, ”, ‘Seat’, ”, ‘publish’, ‘closed’, ‘closed’, ”, ‘seat’, ”, ”, ‘<date>’, ‘<date>’, ”, 0, ‘http:// … ;p=23’, 0, ‘carbrands’, ”, 0),
    (23, 1, ‘<date>’, ‘<date>’, ”, ‘Alhambra’, ”, ‘publish’, ‘closed’, ‘closed’, ”, ‘alhambra’, ”, ”, ‘<date>’, ‘<date>’, ”, 22, ‘http:// … ;p=24’, 0, ‘carbrandmodels’, ”, 0),
    (24, 1, ‘<date>’, ‘<date>’, ”, ‘Altea’, ”, ‘publish’, ‘closed’, ‘closed’, ”, ‘altea’, ”, ”, ‘<date>’, ‘<date>’, ”, 22, ‘http:// … ;p=25’, 0, ‘carbrandmodels’, ”, 0),
    (25, 1, ‘<date>’, ‘<date>’, ”, ‘Altea XL’, ”, ‘publish’, ‘closed’, ‘closed’, ”, ‘altea xl’, ”, ”, ‘<date>’, ‘<date>’, ”, 22, ‘http:// … ;p=26’, 0, ‘carbrandmodels’, ”, 0),
    (26, 1, ‘<date>’, ‘<date>’, ”, ‘Arosa’, ”, ‘publish’, ‘closed’, ‘closed’, ”, ‘arosa’, ”, ”, ‘<date>’, ‘<date>’, ”, 22, ‘http:// … ;p=27’, 0, ‘carbrandmodels’, ”, 0),

    I’ve set up a relationship for the vehicles, and I’m trying to make a conditional relationship when; the brand has a value I’d like to show another relations field for the models only related to the selected brand.

    How can I achieve this with ACF Pro?

  • There are no examples of doing this with a relationship field.

    You need to build custom JavaScript for ACF.

    You need to add an action that fires when the first field is changed that does an AJAX request to get values to populated the other field.

    This is easier to do using dynamically populated select fields.

  • Hmm, so where would I put this? I’m in the admin area … so I’d have to make some kind of plugin for this, or?

  • Or should/could I maybe create a custom Taxonomy consisting of the car models and somehow inject that?

  • You can build it in the theme or build a plugin. I’ve don’t both. I don’t do things like this often, I’ve had maybe 2 clients that have needed something like it. The example I linked to uses 2 different CPTs, but it could be done with parent/child posts or CPTs and taxonomies. It’s a pretty basic example, the important part is done by the AJAX requests and the population of the select fields from the right data.

  • Well, I think I’m in for bumpy ride here. First I had to change:

    var myACFextension = acf.ajax.extend({

    var myACFextension = new acf.Model({

    to get the script to not throw: acf.ajax.extend is not a function error.

    Now, I’m facing this:
    e.$el is undefined

    I guess these scripts haven’t been updated in a while, right?

  • Hmm, seems to partially work (needs stripping of weird markup mess for the excerpt) when changing:

    data = this.o;


    data =

    by checking the ACF API …

  • There are 2 versions of the JavaScript. One is for ACF prior to version 5. You want the file dynamic-select-on-select.js which is for acf >= 5

  • Now, those hard coded field ids (field_579376f522130) worries me a bit. These will have to change with every time I need to make this modification, right?

    It seems non dynamic … so, I guess I’ll have to that the long road and learn to actually extend ACF Pro itself … sigh!

  • There really isn’t a way to make the field keys dynamic.

    You need to have the exact fields that you want to act on. Even when using the ACF JS API you must provide a field key to get a field.

    You could potentially localize the script with the field keys needed, but that just transfers the hard coding to the server PHP rather than the JS code.

    You could potentially create a site option to define the fields that will be used, but that would make even more work.

    If this was something that I would be using many times I would build a plugin and define the fields with PHP so that the field keys would never change.

  • True. I’ll see what I can come up with.

    To me this feature seem like mandatory, as selecting one item in one dropdown have an effect on a second drop down with related options seem like old school user interface.

    It seems weird functionality like this should turn into an it project.

  • Tying the values in one field to the selection in another has never been included in ACF.

    ACF has conditional logic. In this case the way to do this with ACF would be to have multiple fields all conditional on the selection of the first field and each of these fields would be set up to hold the values to use dependent of the first field value. Not very dynamic and not very scalable, but it is what it is.

    I do not know of a single custom fields plugin that can tie two fields together in this way out of the box.

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.