Echo taxonomy select value
-
I know that to echo a value of select field i use:
$options = protags_taxonomy(); $key = get_post_meta($listmesettings_id,'_lmsets_names_maintagshow', true ); echo isset( $options[ $key ] ) ? $options[ $key ] : $options[' '];I understand protags_taxonomy() is getting the options, but how do i do that when i use taxonomy as the options for a select field?
-
Not quite following, based on what you’ve provided so far.
What would help is knowing and preferably seeing an example of what is getting stored in the
$optionsvariable, and what the$keyvalue is. Also beneficial is the CMB2 config so we could at least implement ourselves and tinker with what gets saved.If it was a reguler select (not taxonomy select), for example:
$workdays_box->add_field( array( 'name' => 'ימי עבודה '.$i, 'id' => '_pf_workdays'.$i, 'type' => 'select', 'default' => ' ', 'options_cb' => 'workdays_select_options', ) ); function workdays_select_options() { return array(' ' => ' ','Mo, Tu, We, Th, Su' => 'ראשון - חמישי','Mo, Tu, We, Th, Fr, Su' => 'ראשון - שישי','Tu, Su' => 'ראשון ושלישי','Mo, We' => 'שני ורביעי','Su' => 'ראשון','Mo' => 'שני','Tu' => 'שלישי','We' => 'רביעי','Th' => 'חמישי','Fr' => 'שישי','Fr Plus' => 'שישי וערבי חג','Sa' => 'מוצ״ש',); }and i would want to echo the value of the selected option i know i would use:
$options = workdays_select_options(); $key = get_post_meta( $post->ID, '_pf_workdays'.$wdi, true ); echo isset( $options[ $key ] ) ? $options[ $key ] : $options[' '];where
workdays_select_options()is the function that i use in the config to echo the options of the select field.Now i use select_taxonomy field, and the field config i use is:
$lmsets_names_box->add_field( array( 'name' => 'maintag', 'id' => '_lmsets_names_maintagshow', 'taxonomy' => 'protags_taxonomy', 'type' => 'taxonomy_select', ) );where
protags_taxonmyis the slug of the taxonomy i want to use it’s terms as options for that select field.How would i echo the value of option selected in that field?
Just notice that i edited last post (incase you get the first version by mail).thanks.
Ah, I see the problem.
Note next to the field types that start with “taxonomy_”:
* Default Category/Tag/Taxonomy metaboxes replacement.So while yes, they are conveniently providing a list of terms that you can select and save based on the taxonomy provided, it is meant to be a replacement of the default term assignment metaboxes, and simply sets the terms for the post. It does NOT store the selections as post meta or term meta like you’re expecting.
I would look at https://github.com/CMB2/CMB2/wiki/Tips-&-Tricks#a-dropdown-for-taxonomy-terms-which-does-not-set-the-term-on-the-post and see where this takes you.
Michael is right, but you don’t need to create a custom field type if you want to get access to the taxonomy term values. You would use the built-in term-getting functions, e.g.
$terms = get_the_terms( $post->ID, 'protags_taxonomy' );.Thanks. Ended with:
get_terms( array('taxonomy' => 'protags_taxonomy','fields' => 'id=>name',) );(Both for the config options and for the echoing code).
-
This reply was modified 8 years ago by
Begin.
Hi there!
You are writing at a good time as Im having this issue – If I understand correctly, you are using the taxonomy_select (which DOES save the term to an object/custom post type, but does not save a term_id if I understood the doc) and you are able to retrieve the value?
In my case:
I have a custom post type Artworks with a Artist custom taxonomy, and I’m using the taxonomy_select to attach Artists to Artworks but I would like to retrieve the name of the Artist.I tried the solution with get_the_terms but Im not getting anything, just and empty Array. Did I understand correctly your initial issue?
Thank you!
-
This reply was modified 8 years ago by
elisafern.
Essentially taxonomy_* field types are replacements for the default metaboxes for categories/tags/taxonomies, and save the data in the exact same way: as terms on the post. It doesn’t save anything as post meta.
So that means all of the term/taxonomy based functions that you can use to get content/info from a post would still work.
Not quite sure why you’re receiving an empty array, as
get_the_termshas this for its return types:Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.
Hey Michael!
I noticed an error I made in the loop and now it is working great.
Very Happy!Thanks again to all! 😉
Another satisfied customer *fist pumps*
-
This reply was modified 8 years ago by
The topic ‘Echo taxonomy select value’ is closed to new replies.