• Resolved nagman

    (@nagman)


    Hi,

    I would like to update a multicheck taxonomy field from the front-end.
    I know I have to use the update_post_meta() function (I used it with other custom fields and it worked very well) but I can’t figure out how to update a list of checkboxes.

    I tried to update the field with an array of true/false values (like array(true, true, false, false, false, true, etc.)) but it didn’t work, and I tried with an array of values that must be the one checked (like array(‘category1’, ‘category2’, ‘category9’, ‘category’16’)) and still not working.

    I asked the guys who developed the theme I’m using and they redirected me to your plugin, as they were using it in their theme.

    I hope I make myself clear. Do you have any clue for my issue?
    If you need, I can create a user so you can check directly in the admin panel.

    Quentin

    https://www.ads-software.com/plugins/cmb2/

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

    (@tw2113)

    The BenchPresser

    Can you provide the all the code you’re using for this?

    Plugin Author Justin Sternberg

    (@jtsternberg)

    The CMB2 taxonomy fields do not store/retrieve any data from post meta. They use the taxonomy term APIs, get_the_terms() to retrieve, wp_set_post_terms() to set.

    Thread Starter nagman

    (@nagman)

    The code I’m using is:

    update_post_meta( $postID, ‘listing_expert_cats’, json_decode($_POST[‘categories’]) );

    where ‘listing_expert_cats’ is the custom field slug (a multicheck taxonomy field) and ‘categories’ is an array(true, false, true, true, etc.).

    I also tried with an array(‘category1’, ‘category3’, ‘category4’, etc.).

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you also provide all the code used for CMB2 to create your frontend form? More than just the line that’s meant to save the post meta.

    Thread Starter nagman

    (@nagman)

    I also tried with wp_set_object_terms( $postID, $categories, "listing_categories"), where $categories is an array(285, 286, 288, 295) and listing_categories the taxonomy linked. It didn’t work.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Having absolutely everything, including the code being used to create the metaboxes and submission form will help us a lot more and give us the complete picture. It’d also allow us to recreate on our own dev installs so we can test it out in real time.

    Thread Starter nagman

    (@nagman)

    You don’t need the html form to understand, as I don’t use it for now because I’m doing all the test stuff directly with php :

    if(is_singular(“expert”)){
    if($_POST[“submit_button”]){
    $postID = get_the_ID();

    $categoriesToSave = array(295, 377, 281, 287, 278);
    wp_set_object_terms( $postID, $categoriesToSave, “listing_categories”);
    }
    }

    I simplified the code because the rest is unimportant. But all my code works fine except wp_set_object_terms().

    Also, I can’t give you the code used to create the metabox because I created it in the admin panel. However I can give you the parameters used for the field:
    id: cats
    type: multicheck taxonomy
    taxonomy: categories
    metabox: expert: categories

    Hope that will help you to understand.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’d most definitely argue that it is still relevant that we have a copy of the CMB2 code being used to create all of this, because we could recreate on our own local sites and use debugging tools to see what’s going on, like the metaboxes receiving data from WP in a way that it’s not expecting, causing it to not display multiple checkboxes in the way you, the user, is expecting. We could also use your example code above to see if the function calls are returning what they should be. For instance, get_the_ID(); is a loop function, but if you’re using it in the admin, and not in regards to a WP_Query object, it may not be returning the right thing, or anything.

    As is, with zero other context, your code above theoretically should set the terms just fine. So we’re back to needing other bits of code to see how it’s coming back into the metabox(es).

    We’re not yanking your chain here in any way, we’re trying to efficiently help you.

    Thread Starter nagman

    (@nagman)

    Ok, I understand. Where do I find the CMB2 code generated by the plugin?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    This would be code you’re setting up yourself, similar to the code found in this callback from the example functions file that comes with CMB2

    https://github.com/WebDevStudios/CMB2/blob/master/example-functions.php#L109-L140

    As a possible alternative, and just in case, is this a plugin you installed from somewhere else that happens to be using CMB2 for its purposes?

    Thread Starter nagman

    (@nagman)

    Oh, yes. That’s true: I’m using the superlist theme, which integrates CMB2. And the plugin is directly managed into the admin panel.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The Superlist theme support channel may be the best place to go in that case, to be honest. They’re going to know their setup more than we will, for this part.

    I do find it odd that you’re having to cover part of things, while they apparently cover a different part, unless you’re just utilizing the fact that they have it bundled, and are doing your own thing.

    Thread Starter nagman

    (@nagman)

    Actually, the site I’m changing already had the superlist theme. Otherwise I would never install a wordpress theme in order to change it (to many issues and complications – much better to create the theme on my own). And the superlist theme doesn’t have any documentation for developers.

    I asked their support team and they redirected me here.

    But I’ll ask them the code you’re asking me.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Curious why they are sending you our way when, as far as I’m aware at least, they’re the ones that set up the metaboxes. Alas, I’ll be around.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Update multicheck taxonomy from front-end’ is closed to new replies.