Support » Plugin: CMB2 » Taxonomy field (ACF) to taxonomy_multiselect (CMB2)

  • Resolved parakeet

    (@parakeet)


    Early migration of basic ACF field types to CMB2 is going quite well.

    But I could also do with some pointers…

    I have an ACF Taxonomy field named “userorg” on a User edit page. It is used to indicate the company a user belongs to, wherein “company” is a taxonomy and terms are the companies.

    With the following CMB2 code…

    $cmb_user->add_field( array(
    		'name'     => esc_html__( 'Test Taxonomy Multi Checkbox', 'cmb2' ),
    		'desc'     => esc_html__( 'field description (optional)', 'cmb2' ),
    		'id'       => 'userorg',
    		'type'     => 'taxonomy_multicheck', // Or <code>taxonomy_multicheck_inline</code>/<code>taxonomy_multicheck_hierarchical</code>
    		'taxonomy' => 'company', // Taxonomy Slug
    		// 'inline'  => true, // Toggles display to inline
    	) );

    … the company terms list is shown on the User edit page – but the ‘userorg” term value is not showing as selected.

    This is likely because ACF stores the wp_usermeta meta_value for this record as a longtext serialised thing…

    a:1:{i:0;s:5:"13296";}

    The term ID is in there, 13296.

    In fact, if I show this field as type “text” instead of “taxonomy_multicheck”, what it shows in the box is “13296”, without the JSON stuff.

    Is there any way to leverage this so that CMB2 can properly pick up the term ID as this field type wants?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The Taxonomy_* fields aren’t meant to simply list content from a taxonomy, they’re meant to replace the taxonomy metaboxes for the given post editor screen and save as taxonomy information.

    I would suggest using https://github.com/CMB2/CMB2/wiki/Field-Types#multicheck-and-multicheck_inline and instead of having a hard-set array of options, instead use the options_cb field documented at https://github.com/CMB2/CMB2/wiki/Field-Parameters#options_cb to fetch the taxonomy and terms to populate the dropdown. That way the taxonomy data gets stored as meta on the user.

    Thread Starter parakeet

    (@parakeet)

    Indeed, that’s what I’m trying to do…

    Rather than simply *list out* the taxonomy terms, I’m wanting to put the taxonomy’s term selector on a User edit page.

    And I can do that with either taxonomy_select or taxonomy_multiselect. It shows the picker list.

    The trouble is, it’s not showing a selected value – that is, it’s unable to pluck 13296 out of the “userorg” value ACF stored as a:1:{i:0;s:5:"13296";}.

    In fact, does CMB2 do User-to-tax-term stuff entirely differently?

    I’ve even spun up a new taxonomy_multicheck field, “raorguser”, to show the same “company” taxonomy selector and make a save for a checked value (so as not to overwrite the one against “orguser”). But I see no such value getting saved in the database for “raorguser”…

    What I think I do see is something in wp_term_relationships… object_id 5442 (that’s my user) relating to term_taxonomy_id 13296 (that’s his company).

    That’s pretty different from ACF using a serialised JSON field to just go “oh, yeah, it’s 13296”. (Although, for all I know, ACF also does that). I guess this is how CMB2 does it? (And maybe how WordPress would do it, were Users formally allowed to relate to taxonomy terms?)

    If so, would you say using wp_term_relationships is the proper way? Where can I read more about this?

    And how on earth could I migrate this ACF field over to CMB2-land?

    Migration may not be as straightforward as I thought based on just looking at plaintext fields.

    Thanks.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    To clarify, the taxonomy_* fields explicitly try to set terms to posts, using WP core functions like https://developer.wordpress.org/reference/hooks/set_object_terms/ and whatnot. They don’t use post/user meta functions.

    This is why i pointed you to the idea of just a standard multicheck field, where you can populate using whatever term fetching functions you want to use, via the options callback.

    We actually don’t explicitly do anything user-to-tax term, it just happens to be able to be done, when you consider the idea that you’re basically just storing term information as part of user meta.

    Thread Starter parakeet

    (@parakeet)

    As I understand it, “multicheck” is for pre-populating available checkboxes from a defined list of options.

    The term I am setting for users is one of 2,000+ in the taxonomy.

    So, I think you’re suggesting, rather than a manually preset list of “options” (array), I should use “options_cb” with, say, ‘get_my_company_terms()’… ?

    
    function get_my_company_terms() {
    			$terms = get_terms( array(
    				'taxonomy' => 'company',
    				'hide_empty' => false,
    				) );
    				return $terms;
    		  }
    
    

    That result is a WP_Term object, though, not a string. It throws an error when the admin echoes out that part of the box. What are you thinking the value should be? Ie somehow reconstitute the array so it contains only a bunch of term IDs as strings… ?

    Thanks.

    • This reply was modified 1 year, 3 months ago by parakeet.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    multicheck fields are meant to allow choosing 1-to-many choices.

    Yes, use the callback to do some extra logical fetching to populate and return a key => value pairing array. It can be whatever pairing you want, I wouldn’t just return $terms.

    If you’re wanting to have the value saved be the term ID, perhaps try this:

    function get_my_company_terms() {
    	$terms = get_terms( array(
    		'taxonomy' => 'company',
    		'hide_empty' => false,
    	) );
    	
    	$options = [];
    
    	foreach ( $terms as $term ) {
    		$options[ $term->term_id ] = $term->name;
    	}
    
    	return $options;
    }
    

    This would return an array where the key is the term ID and the value shown in the dropdown is the term name.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Taxonomy field (ACF) to taxonomy_multiselect (CMB2)’ is closed to new replies.