Support » Plugin: CMB2 » Taxonomy selec field not saving in page option

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    If you’re expecting the data to show up as post meta, you’re going to be using the wrong field type for that. The taxonomy_* field types are meant to be used as replacements for the default taxonomy-based metaboxes, and set term data for the post. They don’t save as custom fields. You’ll need to utilize standard ‘select’ fields and populate with term data for a custom field based version.

    If you need, I can hunt down some links to some resources for setting up the latter.

    Hi Michael, my intention is save term id for custom wp_query, something like:

    
    function custom_home_loop($category) {
            $args = array(
                'post_type' => 'post',
                'posts_per_page' => '5',
                'order' => 'DESC',
                'orderby' => 'date',
                'meta_query' => array(array('key' => '_thumbnail_id')),
                'category_name' => $category,
            );
    ...
    
    Plugin Author Michael Beckwith

    (@tw2113)

    From the looks of that snippet of code, you’d be needing the term slug, more than ID, assuming you’re referring to $category being passed in and used. How you were hoping/planning to grab the $category variable value will dictate what type of field is needed. For example, https://developer.wordpress.org/reference/functions/get_the_terms/ vs https://developer.wordpress.org/reference/functions/get_post_meta/

    Hi Michael, solved:

    function select_category_id() {
        $cats = get_categories('category');
    
        $cat_list = array('');
        foreach ($cats as $cat) {
            $cat_list[$cat->term_id] = $cat->name;
        }
        return $cat_list;
    }
    
    $themeoption->add_field(array(
                'name' => __('Select Category', 'omni'),
                'id' => '_category_home',
                'show_option_none' => true,
                'type' => 'select',
                'options' => select_category_id()
            ));
    

    Thanks!

    • This reply was modified 1 year, 10 months ago by  Angelo Rocha.
    Plugin Author Michael Beckwith

    (@tw2113)

    Huzzah!

    The only thing I’d change is the following:

    'options' => 'select_category_id'
    

    If I recall correctly, this will prevent the function from executing all the time, and instead make it run only when needed. Passing in the function name treats it as a callback that CMB2 then calls itself when ready. Leaving it as select_category_id() calls the function every time the file is loaded. Some performance gain available.

    Justin could explain it better than me, I wager, but we’re in a good position as a whole here.

    Correct! Thanks again.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Yep, but use the ‘options_cb’ parameter. Using a callback for the ‘options’parameter is (unofficially) deprecated. But as Michael said this will optimize so that the query is only run at the last minute before the field is displayed.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Taxonomy selec field not saving in page option’ is closed to new replies.