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

    (@tw2113)

    The BenchPresser

    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.

    Thread Starter Angelo Rocha

    (@angelorocha)

    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 Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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.www.ads-software.com/reference/functions/get_the_terms/ vs https://developer.www.ads-software.com/reference/functions/get_post_meta/

    Thread Starter Angelo Rocha

    (@angelorocha)

    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 8 years, 2 months ago by Angelo Rocha.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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.

    Thread Starter Angelo Rocha

    (@angelorocha)

    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.