Support

Account

Home Forums General Issues Re-using fields in different groups? Can they simply have the same name?

Solved

Re-using fields in different groups? Can they simply have the same name?

  • I have been using Toolset with WordPress and before that I was a Joomla/Seblod user, so I was caught a bit off guard by the fact that ACF doesn’t make fields re-usable (from what I can tell).

    In Toolset, you create a field group, create fields in that group, hit save; and when you create the next field group, you can choose from the fields you have created in all previous field groups, and this can be repeated. J!Seblod is the same from my recollection. The groups seem to be used only for assigning (location)… which post-types/taxonomies/etc are using which fields for the postmeta. When you create a view, all of the saved values of the field that you select by name are filtered and collated the same.

    I can see that in ACF you can’t choose from fields you’ve already created. I suspect there are may be good reasons for this, so you instead have to create new fields in each group. But you can create new fields with the same name… the key will be automatically set for uniqueness, but the name can be the same. Is this basically the same thing?

    Will the fields be filtered and collated the same despite the different field keys, and if so are there any long-term issues to consider when using the same name in multiple field groups?

    If that’s not clear, here’s the case that had me take a step back: creating a product catalog with technical specification fields that can be used for a variety of products, which themselves could belong to one or more product types (taxonomy, we’ll call each category a-to-z for simplicity). We don’t want the user to be confronted with fields that don’t specifically apply to the type of product they are creating, so we need the fields filtered by the product type.

    You may have ‘wattage’ that applies to half of the product types and not to the other half. You may have ‘frequency’ that applies to all but one product type. You may have ‘max dB’ that applies to only one product type. And so forth.

    Trying to achieve this without reusing a single field, so that each product is using the correct field with the same field name and the same field key as other products that share the field is burdensome:

    one field group is assigned to a,b,c,d,e,f,g,h,i,j,k,l product types,
    one field group is assigned to b,g,s,v,z product types,
    one field group is assigned to d,e,g,i,k,m,n product types,
    one field group is assigned to e product type,
    one field group is assigned to q,r,s,t,u,v,w,x,y,z product types,
    one field group is assigned to l,t,r,s product types,
    one field group is assigned to l,t,r product types,
    and so on and so on… not even finished yet.

    This can’t be the right way to do it…

    Most of the product types are overlapping to ensure the fields aren’t used more than once, but this makes it difficult to navigate the field group admin page, and it really makes the post editor a mess with multiple groups to the same product type.

    If it were on Toolset the field groups would be: one for each, a-z, and the fields would be repeated on each, ensuring each product type has only one group (not messy on the post editor screen or the field group admin page).

  • It is possible to achieve this without created multiple field groups and multiple fields of the same name.

    The best, easiest why to achieve this is.

    1) Instead of using the default meta boxes for assigning taxonomy terms to post you use an ACF taxonomy field in the field group.

    You remove the taxonomy metabox in WP by setting the “meta_box_cb” argument to false.

    In the ACF taxonomy field you turn on both the “Load Terms” and the “Save Terms” options.

    2) For all of the other fields you use conditional logic to determine if a field should be shown based on the taxonomy field.

    This can be a little difficult using the ACF native conditional logic because you have to check if the term ID is > or < a given value.

    Conditional logic can be extended if you want to be able to choose terms, this has been done but I don’t have any specific examples to point you to.

  • I mostly understand this and it seems like an excellent solution.

    There is an issue I’m running into however…

    I should have mentioned the product types include both parent and child, and there can be instances where the product belongs to multiple product types.

    It seems I can only get the conditional logic to work if the Product Type has only one option selected and the Product Type Taxonomy Field is set to a single value. Radio Button or Select, not checkbox or multi-select.

    I don’t mind adding conditional logic for both a parent Product Type (A with ID 4) and a child Product Type (B with ID 5), but I can’t see how it would work…
    Product Type (field) Selection is Greater than 4 and Less than 6 isn’t returning Product Type B when both B and A are selected.

    Is there a way around this?

    “You remove the taxonomy metabox in WP by setting the “meta_box_cb” argument to false.”

    In the edit taxonomy page for the Product Type taxonomy I set Visibility->Meta Box to “No Meta Box”, but I don’t see any difference. Any clarification would be greatly appreciated.

  • Yes, you’ll need to add some custom conditional logic, I don’t have an answer for you but you will find some solutions to start from in [FORUM SEARCH RESULTS]

  • Doesn’t seem like anything I’ve read will give me the desired result.

    Circling back to the original question: Can fields sharing the same name be filtered and collected together despite having different field keys. If so are there any long-term issues when using fields of the same name for the same purpose in several different field groups?

  • Yes, field can have the same name.

    ACF saves the fields in _postmeta table with a meta_key that matches your field name. As long as a field name is not duplicated there isn’t an issue.

  • “As long as a field name is not duplicated there isn’t an issue.”

    Sorry, For clarification over the use of Field Name, I’m not talking about the label.

    Example…

    Field Group One would contain:
    Field Label “Wattage”, Field Name “wattage”, Field Key “field_01xtine3m”
    Field Label “Max DB”, Field Name “max_db”, Field Key “field_z9tebe9n5”
    Field Label “Frequency”, Field Name “frequency”, Field Key “field_59ddt5qec”
    Field Label “Avg Temp”, Field Name “avg_temp”, Field Key “field_n5bew9te9”
    Field Label “Impedance”, Field Name “impedance”, Field Key “field_nti5bxte7”

    Location Product Type is equal to “b”

    Assigned to

    Field Group Two would contain:
    Field Label “Wattage”, Field Name “wattage”, Field Key “field_tit501xqec”
    Field Label “Max DB”, Field Name “max_db”, Field Key “field_ebz9tne3m”
    Field Label “Frequency”, Field Name “frequency”, Field Key “field_de59d9n5”
    Field Label “Pressure”, Field Name “pressure”, Field Key “field_te57t501x”
    Field Label “Inductance”, Field Name “inductance”, Field Key “field_te9915s0”

    Location Product Type is equal to “c” or Product Type is equal to “d”

    Field Group Three would contain:
    Field Label “Wattage”, Field Name “wattage”, Field Key “field_ti9d9n5tec”
    Field Label “Max DB”, Field Name “max_db”, Field Key “field_eb501z9xq”
    Field Label “Frequency”, Field Name “frequency”, Field Key “field_dee3m5tn”
    Field Label “Strand Count”, Field Name “strand_ct”, Field Key “field_dfa36xtn”
    Field Label “Channels”, Field Name “channels”, Field Key “field_dtn6ga3x”

    Location Product Type is equal to “e”

    Would this work fine or lead to issues down the road?

  • What I mean by not duplicated is that you can have the same field name available more than once as long as they don’t appear more than once while editing. So for example there will never be more than one “wattage” field displayed.

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

You must be logged in to reply to this topic.