mustardBees / cmb-field-select2

Select2 field type for Custom Metaboxes and Fields for WordPress
93 stars 46 forks source link

How do I retrieve data from a pw_multiselect field for which populated options come from a custom taxonomy terms? #32

Closed monecchi closed 8 years ago

monecchi commented 8 years ago

I'm getting a hard time to retrieve the data from a pw_multiselect field for which the populated options come from a custom taxonomy terms. The issue I'm facing is that the pw_multiselect field lies within a repeatable group, and I'm not sure how to output it with along my current foreach loop logic.

// Set the field
    $cmb_group->add_group_field( $group_field_id, array(
        'name'    => __( 'Ingredients, flavours:', 'my theme' ),
        'id'      => 'menu_item_ingredients',
        'desc' => __( 'Which ingredients does this food menu item is made of? - Select and drag ingredients to reorder.', 'pivot'),
        'placeholder' => __( 'Select the ingredients:', 'pivot' ),
        'type'    => 'pw_multiselect',
        'options' => iweb_get_cmb_options_array_tax( 'ingredient_tag' ), // ingredient_tag is a registered custom taxonomy - it acts as real post tags
    ) );

The field is properly populated, and although it does not allow me to reorder, it is properly saving the data.

pw_multiselect

The repeatable group is set and working properly, I can grab the fields values as such:

// repeatable group
$food_prices = get_post_meta( get_the_ID(), '_prefix_food_price_group', true );

foreach ((array) $food_prices as $key => $value) {

    if (!(array_key_exists('price_legend', (array) $value) ) ) {
    } else {
        $price_legend = (array) $value['price_legend']; 
    }

    if (!(isset($value['price_amount']))) { 
    } else {
        $sale_price =  esc_html($value['price_amount']);
    }

    if (!(isset($value['price_desc']))) {
    } else {
        $price_desc =  esc_html($value['price_desc']);
    }

    echo $price_amount;
    foreach ( $price_legend as $legend ) {
        echo '<div class="text-center">' . esc_html($legend) . '</div>';
    }
    echo $price_desc;

}

I'm trying to check for the pw_multiselect field within the foreach loop as follows:

if (!(isset($value['menu_item_ingredients']) && $value['menu_item_ingredients'] !== '' )) { 
} else { 
    $ingredients = (array) $value['menu_item_ingredients']; 
}

But if I try to output it like the following it returns nothing:

foreach ( $ingredients as $ingredient ) {
    echo '<div class="text-center">' . esc_html($ingredient) . '</div>';
}

As long as the options are real taxonomy terms, do I have to use get_the_term_list() instead of get_post_meta()?

Thanks in advance.

mustardBees commented 8 years ago

Hi @monecchi. I can't see anything glaringly obvious. I'll have some time tomorrow afternoon to test this out using your example.

I'd expect $ingredients to be an array of taxonomy term ID's. You'll then need to plug that ID, along with the taxonomy slug into get_term() in order to grab the term name.

You could print_r() the $ingredients variable to see if you are indeed getting back an array of ID's?

monecchi commented 8 years ago

Hey @mustardBees, thanks for the input! I've checked that out with print_r() to see what I was getting, and the response is it returns nothing.

I've inspected the field on the WP Admin side with the chrome developer tool and noticed the [menu_item_ingredients][ ] is empty after saving, although it looks like options are in place as per the image bellow. Shouldn't [menu_item_ingredients][ ] be saving the selected options on it or am I confusing things here?

pw_multiselect_options

I'll debug my code and come back here for details.

mustardBees commented 8 years ago

What version of CMB2 are you running?

monecchi commented 8 years ago

@mustardBees I'm using the 2.2.1 trunk version from the cmb2-rest-api branch.

mustardBees commented 8 years ago

@monecchi The field works against the current stable release (CMB2 2.2.1) from the WordPress Plugin repository.

I've tested against the cmb2-rest-api branch. Unfortunately I couldn't replicate the issue you've seen... mainly because I didn't get that far. There is a fair amount of active development going on. For me, the concat_items() method has been moved, so the field is unable to generate the HTML options. It was moved in WebDevStudios/CMB2@1b3e251f8db429b3930e44ff43495838a9a39744.

I'm not sure what the plans are for this branch in terms of further breaking changes/time frames. It might be worth waiting on CMB2 to stabilize before working on an update.

monecchi commented 8 years ago

Hey @mustardBees, thank you for your input and for the tests you've performed. I'll try to stick to the stable version again as this will save me from pulling my hair out. I'll wait for the WP Rest API support, which is still in the works, so as per your tests I might get it working with CMB2 stable version.

mustardBees commented 8 years ago

@monecchi Can you try again with version 3.0.1? I've tested against CMB2 2.2.2.1. Let me know if you have any further trouble.