admin管理员组

文章数量:1315069

I have a custom taxonomy A with some terms (A1, A2,A3, etc) and a second custom taxonomy B. I want to create a custom field in taxnomony B which will be a dropdown menu with the terms from taxonomy A. I am able to create custom fields with texts and to get the terms from taxonomy A with get_terms(). The get_terms() function, returns an array of object (every object is a term). I am not sure how to build the dropdwon. How can I connect them? (I want to do it programmatically so that I can use the code for more than one sites) Thank you

I have a custom taxonomy A with some terms (A1, A2,A3, etc) and a second custom taxonomy B. I want to create a custom field in taxnomony B which will be a dropdown menu with the terms from taxonomy A. I am able to create custom fields with texts and to get the terms from taxonomy A with get_terms(). The get_terms() function, returns an array of object (every object is a term). I am not sure how to build the dropdwon. How can I connect them? (I want to do it programmatically so that I can use the code for more than one sites) Thank you

Share Improve this question edited Nov 22, 2020 at 14:24 marissalianam asked Nov 21, 2020 at 21:43 marissalianammarissalianam 211 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You can use ACF (Advanced Custom Fields) plugin to do this. Select Taxonomy field and set the field group to appear where you need to.

I'm pretty sure the free version of the plugin has this field.

Instead of showing custom field with text, create a custom field with select dropdown.

  1. Populate that dropdown options with get_terms function (term_id as value)

  2. Add multiple attribute to select if you need to store more than one term relation.

  3. Store values as array of term ids

  4. On frontend loop trough saved array and use get_term function to get term object data.

p.s. on 4) you should check if saved term id exists, if not then you should take care of it (delete the relation or ignore on loop I guess)

本文标签: Custom field with terms from another taxonomy programmatically