I use bunch of custom fields in a custom post type. However, I need some of the custom field to be used as taxonomy as well. For example,
I have a custom field called Place and A custom taxonomy called States. So the value that user insert into CF State (eg. ohio), also automatically become the term in taxonomy States.
The States taxonomy inserting function should be run in behind automatically. No need to display to the user.
How do I achieve this?
Thanks
Arnav Joy answers:
you can use following function
<?php wp_insert_term( $term, $taxonomy, $args = array() ); ?>
Francisco Javier Carazo Gil answers:
You have to do a call to wp_insert_term when you are saving or updating value of custom field.
Francisco Javier Carazo Gil comments:
You have to use this function at the same time you save your meta with update post meta: http://codex.wordpress.org/Function_Reference/update_post_meta
If the values has changed, you have to be careful, first check if new value exists with has_term http://codex.wordpress.org/Function_Reference/has_term and later:
* if exist in the same post: do nothing
* if not exist: delete the old term with old value and create a new one
house.tc comments:
The problem is I am using plugin to create those custom meta field. How do I use update_post_meta? Hook in function.php? It would be great if you have some example of the codes. thanks
John Cotton answers:
Can I ask why you want to do this?
If you're going to set a taxonomy value, surely the custom field is redundant? (Or vice versa)
You can query by taxonomy and query taxonomy by post....I can't think of a reason why you want this :)
house.tc comments:
Well, since there are no plugin to create custom taxonomy input panels (like dropdown, button, etc) ,create custom meta field is much easier with plugin. Besides, there are many fields my post-type, it would be very time consuming to create each taxonomy manually.
However, taxonomy is easier to categories or sort in the front end. Custom field in the other hand is lack of this advantage.
I know it sound stupid to do this, but be frankly I am not too familiar with wp, so I couldn't come up a better idea.
John Cotton comments:
Since you're going to have to write some code somewhere to do what you want, you'll find it much easier to code what you want directly without the custom field plugin....
Have a look at this:
[[LINK href="http://codex.wordpress.org/Function_Reference/add_meta_box"]]http://codex.wordpress.org/Function_Reference/add_meta_box[[/LINK]]
It shows you how to code your own meta box. With it, you can add all sorts of user interface to get what you want. Better code....easier for the user....