Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taxonomy picker on User edit saves mysteriously #1489

Open
robertandrews opened this issue Apr 4, 2023 · 3 comments
Open

Taxonomy picker on User edit saves mysteriously #1489

robertandrews opened this issue Apr 4, 2023 · 3 comments

Comments

@robertandrews
Copy link

Describe the bug

When a taxonomy_multicheck_hierarchical is added to 'object_types' => array('user'), the value may be saved... somewhere... but not apparently in wp_usermeta as expected.

I cannot find any trace of the value getting saved in the database, though possibly it is settling as an wp_options transient, not sure.

When the value is unchecked and the User is re-saved, the value stays checked on reload.

Overall very odd behaviour.

If User cannot simply take a taxonomy field, what is the correct procedure for both saving and retrieving values?

Steps to reproduce

Register a metabox, add a taxonomy field to user.

CMB2 Field Registration Code:

<?php

add_action('cmb2_init', 'context_register_user_metabox');

function context_register_user_metabox()
{

    // Metabox: User Profile
    $cmb = new_cmb2_box(array(
        'id' => 'user_edit',
        'title' => __('User Profile Metabox', 'cmb2'), // Doesn't output for user boxes
        'object_types' => array('user'), // Tells CMB2 to use user_meta vs post_meta
        // 'show_names' => true,
        // 'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option.
        'context' => 'normal',
        'priority' => 'high',
    ));

    // Field: Company taxonomy
    $cmb->add_field(array(
        'name' => 'User Company',
        'id' => 'company',
        'type' => 'taxonomy_multicheck_hierarchical',
        'taxonomy' => 'company',
    ));

}
?>

Your Environment

WordPress 6.2, MAMP, macOS, PHP 7.4.33.

Additional context

Add any other context about the problem here.

@tw2113
Copy link
Contributor

tw2113 commented Apr 4, 2023

The taxonomy_* field types are meant to replace the taxonomy term metaboxes for posts. They're not meant to be used generically-ish to associate taxonomy terms to things.

You'd want to make use of https://github.com/CMB2/CMB2/wiki/Field-Types#multicheck-and-multicheck_inline and https://github.com/CMB2/CMB2/wiki/Field-Parameters#options_cb to fetch the terms in the taxonomy, and return your array with that.

@robertandrews
Copy link
Author

robertandrews commented Apr 4, 2023

Thanks for the pointer.
So, this seems to work...

$cmb->add_field(array(
        'name' => 'Company',
        'id' => 'company',
        'type' => 'multicheck',
        'options_cb' => 'cmb_company_terms',
        'select_all_button' => false,
    ));
function cmb_company_terms($field)
{

    // Get all "company" terms into an associative array
    // Get all terms of the 'company' taxonomy
    $terms = get_terms(array(
        'taxonomy' => 'company',
        'hide_empty' => false,
        'fields' => 'id=>name',
    ));

    return $terms;
}

Screenshot 2023-04-04 at 23 03 31

Screenshot 2023-04-04 at 23 04 12

What do you think?
It's just lacking the hierarchicality of the taxonomy, which taxonomy_multicheck_hierarchical does have.

In ACF, I think it just figures out where to put the data, wherever I might put a taxonomy selector, so I think that's why I hope this would happen here.

--

By the way, unlike for user, I seem to have had some success in setting a taxonomy_multicheck_hierarchical on a taxonomy, so as to allow a term to set a relationship with a term from another taxonomy. When I say "success", I mean this does set the meta in wp_termmeta. Could you flag-up if you see any issues with this?

++

Presumably, checkbox also acceptsoptions_cb? And maybe checkbox saves data plainly, inside of an array?

@tw2113
Copy link
Contributor

tw2113 commented Apr 4, 2023

Majority of the fields all have the items listed at https://github.com/CMB2/CMB2/wiki/Field-Parameters as available. So, yes, checkbox should as well.

Regarding flags, kind of a case of it's working in a way that you could work with, why not. I would need to trace my way through the core code to come up with any sort of answer of how/why it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants