Support

Account

Home Forums ACF PRO Shortcode for fields attached to taxonomies

Solved

Shortcode for fields attached to taxonomies

  • Is there a similar shortcode like described at https://www.advancedcustomfields.com/resources/shortcode/, but for fields attached to a taxonomy? I imagine the shortcode would look something like…

    [acf field="field_name" taxonomy="taxonomy_name" term_id="1"]

    If not, can this be a feature?

  • It would be something like this

    
    [acf field="field_name" post_id="term_1"] // 1 is the term ID
    
  • I have a custom taxonomy called Agents with taxonomy slug agents. I created a custom field group called Agents. The fieldnames for the fields in the group are agent_name, agent_phone, agent_email and agent_picture.

    I tried the above code like so:
    [acf field="agent_name" post_id="agents"]

    This did not return any values. How can I return custom fields attached to the agents taxonomy?

  • See my previous comment. “agents” is not a valid value for post_id. You must supply the ID that ACF needs. This is "term_{$term_id}"

  • Where would I find the id for the taxonomy?

  • You cannot add fields directly to a taxonomy. Fields are added to terms in that taxonomy. You need the term ID of the term where you are entering values into the ACF fields.

  • So how would I go about adding agents to Homes CPT? I thought create Taxonomy called agents and add terms which are the agents names like Jane Doe and John Doe. I attached custom fields to the taxonomy and it all looks good. I just can’t return the agents email, phone, name associated with the Home.

  • What you are trying to do is beyond the scope and capability of the ACF shortcode. The reason, I assume by reading your problem, is to show values from a term that the post in a CPT is assigned to.

    If you want to do this with shortcodes then you must built your own shortcode that can get the post term in your custom taxonomy that is assigned to the post and get the fields from that term.

    https://developer.wordpress.org/reference/functions/add_shortcode/

  • I am assigning an Agents taxonomy to Homes CPT. The Agents taxonomy has custom fields for the agents picture, name, email and phone.

    Agents

    I am limited to the themes abilities to return and show items in the grid. Right now it only allows me to show items of a taxonomy. Ideally I would create a CPT Agents and CPT Homes. Then somehow relate the two and show them in the grid.

    Its a cool idea though right? I mean if I can attach the Agents taxonomy to the Home CPT with custom fields for the taxonomy for agent picture, name, email and phone its an easy solution.

    Unfortunately I don’t have coding capabilities and was hoping you would be able to come up with a solution. (i.e. the shortcode)

  • Hi Steven,

    The shortcode only returns simple text-based values, so if the agent_picture returns an array of image info, then the shortcode will not be ideal for your use case. However, you can probably use it to display an agent’s name, email, and phone, assuming that all those values are stored in text, email, and tel type fields.

    Anyway, I think you are getting the shortcode confused with code that would happen in a code loop that goes through the homes. The logic would look something like this for your use case:

    
    foreach home CPT as Home
        Display Home info
        Get all Agents assigned to Home
        foreach Agents as Agent
            Display Agent info (this is where you can use the shortcode)
        endforeach loop
    endforeach loop
    
Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Shortcode for fields attached to taxonomies’ is closed to new replies.