Support » Plugin: CMB2 » Term meta is saved but it doesn't appear in term edit screen later

  • Resolved Tauno Hanni

    (@taunoh)


    I’ve run into a problem where the metadata is stored but It doesn’t appear in term edit screen later.

    $oi_catalog = new_cmb2_box( array(
                'id' => $prefix . 'catalog_metabox',
                'title' => esc_attr__('Catalog settings'),
                'object_types' => array('term'),
                'taxonomies' => array('product_category'),
                'context' => 'normal',
                'priority' => 'high',
                'show_names' => true
            )
        );
    
        $oi_catalog_group_id = $oi_catalog->add_field( array(
                'id'          => '_cmb_catalog_repeat_group',
                'type'        => 'group',
                'options'     => array(
                    'group_title'   => __( 'Banner {#}'),
                    'add_button'    => __( 'Add banner'),
                    'remove_button' => __( 'Remove banner'),
                    'closed'    => false,
                    'sortable' => false
                )
            )
        );
    
        $oi_catalog->add_group_field($oi_catalog_group_id, array(
                'name' => 'URL',
                'id'   => 'url',
                'type' => 'text_url'
            )
        );

    Am I missing something? I’m running WP 4.5.3, CMB2 2.2.2.1

    https://wordpress.org/plugins/cmb2/

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

    (@tw2113)

    The BenchPresser

    Not missing anything that I can see, but I’m also not managing to recreate at the moment.

    https://cloudup.com/cB_mMndzrcq

    Only part I’ve changed is the taxonomies parameter to match something I had readily available.

    Metadata saves and displays on the term’s edit page thus far.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    <?php
    function yourprefix_register_demo_metabox() {
    	$prefix = 'yourprefix_demo_';
    
    	$oi_catalog = new_cmb2_box( array(
    			'id'           => $prefix . 'catalog_metabox',
    			'title'        => esc_attr__( 'Catalog settings' ),
    			'object_types' => array( 'term' ),
    			'taxonomies'   => array( 'download_category' ),
    			'context'      => 'normal',
    			'priority'     => 'high',
    			'show_names'   => true
    		)
    	);
    
    	$oi_catalog_group_id = $oi_catalog->add_field( array(
    			'id'      => '_cmb_catalog_repeat_group',
    			'type'    => 'group',
    			'options' => array(
    				'group_title'   => __( 'Banner {#}' ),
    				'add_button'    => __( 'Add banner' ),
    				'remove_button' => __( 'Remove banner' ),
    				'closed'        => false,
    				'sortable'      => false
    			)
    		)
    	);
    
    	$oi_catalog->add_group_field( $oi_catalog_group_id, array(
    			'name' => 'URL',
    			'id'   => 'url',
    			'type' => 'text_url'
    		)
    	);
    }
    add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );

    Hi,

    I am also facing same issue. Term value won’t show up in edit fields.

    Way to replicate issue, assuming you have fresh WordPress installation:
    1. Add content to term in term meta fields.
    2. Go to edit fields and you will see content.
    3. Now create custom post and assign term to it.
    4. Again go to term edit field, all information will be lost
    5. After that, content wont show up on any terms.

    I am using WordPress version 4.5.3 and CMB2 version 2.2.2.1

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Still not managing to recreate myself.

    1. Created a new term, saved the url as term meta via the cmb2 code above. Checked that the URL was saved.
    2. Created new post, assigned term.
    3. Re-visited term edit page and saw the provided url still saved.

    Double checking here. Are you sure you’re assigning the same term that you saved meta for, as a term for the post?

    Is it recreatable with all other plugins except CMB2 disabled?

    I am using CMB2 through composer. I tried by disabling other plugins but it didn’t solve issue.

    Can you please help resolve this issue? It is affecting my other sites too. I can send you my theme files.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    sachit, I can do what I can, but first I need to be able to reproduce the issue, which hasn’t happened yet.

    If you could send me the CMB2-specific code and fields setup for your term meta saving, I can try with your version as opposed to what I have above.

    if ( ! function_exists( 'mok_activity_meta_box' ) ) {
    	function mok_activity_meta_box() {
    		#region Activity Details
    		$activity = new_cmb2_box( [
    			'id'               => '_activity_details',
    			'title'            => __( 'Activity Additional Details', '_mok' ),
    			'object_types'     => [ 'term' ],
    			'taxonomies'       => [ 'activity' ],
    			'new_term_section' => true,
    		] );
    		$activity->add_field( [
    			'name' => __( 'Activity Featured Image', '_mok' ),
    			'desc' => __( 'Will be used based on theme (optional)', '_mok' ),
    			'id'   => '_tax_featured_image',
    			'type' => 'file'
    		] );
    		$activity->add_field( [
    			'name'    => __( 'Activity for destination', '_mok' ),
    			'desc'    => __( 'Will be used based on theme (optional)', '_mok' ),
    			'id'      => '_tax_select_destination',
    			'type'    => 'select',
    			'options' => mok_get_terms_list()
    		] );
    		#endregion
    	}
    }
    add_action( 'cmb2_admin_init', 'mok_activity_meta_box' );
    
    if ( ! function_exists( 'mok_get_terms_list' ) ) {
    	/**
    	 * Returns terms list
    	 * @return array
    	 */
    	function mok_get_terms_list() {
    		$terms_list = [ ];
    		$terms      = get_terms( [
    			'taxonomy'   => 'destination',
    			'hide_empty' => false,
    			'parent'     => 0
    		] );
    		foreach ( $terms as $term ):
    			$terms_list[ $term->term_id ] = $term->name;
    		endforeach;
    
    		return $terms_list;
    	}
    }

    Above is my code.

    along with cmb2, there are other packages loaded via composer (icanboogie/inflector, tgmpa/tgm-plugin-activation and aristath/kirki)

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Ok,

    I copied the code above into my mu-plugins file that i’ve been using for CMB2 testing lately.

    I changed the “activity” taxonomy to “actor”, which I had available, for ease of setup. The taxonomy itself shouldn’t be making a difference here. I also changed the “destination” taxonomy to the default “category” taxonomy that comes with WordPress.

    I added a new term, I set a file, and I set a category from the dropdown, hit add. I then edited a post in the post type the “actors” taxonomy was assigned to. I added the newly created term, hit save on the post, and re-checked the taxonomy meta data. Nothing changed. The image and the chosen category term remained selected.

    I then edited an existing term, to add a file/category pair, and then created a new post, set the existing term in that post, and hit publish. Same results, the term metadata remained as set previously.

    At this point, I’m curious if there’s other code interfering somehow and removing the data unintentionally. As is, with the bare-bones setup for the term metabox and whatnot, it’s working.

    Thread Starter Tauno Hanni

    (@taunoh)

    I did some debugging and found that $this->object_type is in mine case “post”. But should be “term”. I couldn’t find a reason why this is so.

    If I comment out this part in code I get it work:
    https://www.dropbox.com/s/7w3s3wz1tdywmvs/Screenshot%202016-08-15%2016.12.28.png?dl=0

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not 100% sure that’s related. I re-added your original config code at the top, set a breakpoint inside that object_type method pointed out, and refreshed the page. My breakpoint got triggered multiple times, including at least once where “object_type” ended up being “post”, but also other blank values and “term” like expected. Possible I just have it being properly called many times (thanks dev site!), but it did still work in the end.

    Just had the same issue and commenting that line solved it.

    From what I understood object_type is not defined when entering that function and so it assumes the default value “post”.

    Tried to see where it’s not being correctly processed as the fields are shown and only the values aren’t but no luck so far.

    I don’t what causing issue but creating term meta before post meta solves this issue for me

    Having another go at this bug I’ve notice that the “options page” fields weren’t displayed when the object_type attribution is commented out.
    I’ve fix this issue by adding a check for the current admin page also in this function.

    /**
     * Returns the object type
     * @since  1.0.0
     * @return string Object type
     */
    public function object_type( $object_type = '' ) {
    
    	if ( $object_type ) {
    		$this->object_type = $object_type;
    		return $this->object_type;
    	}
    
    	global $pagenow;
    		
    	if ( 'term.php' !== $pagenow ) {
    		if ( $this->object_type ) {
    			return $this->object_type;
    		}
    	}
    
    	$this->object_type = $this->current_object_type();
    
    	return $this->object_type;
    }

    Didn’t submitted an issue and PR for this in the repo because I’m not totally sure what’s causing this bug to happen.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Going based on my last comment in this thread, where I noted hitting a breakpoint multiple times for different types of objects, I’d say hardcoding anything that returns early unless of a specific type, would not be good. I imagine the method is multi-purpose and handles many situations.

    True that’s why I think this isn’t the proper solution but it works, just want to bring to your attention that the fix it’s the other way around, it doesn’t allow to return earlier when it’s in terms page.

    The original function:

    /**
     * Returns the object type
     * @since  1.0.0
     * @return string Object type
     */
    public function object_type( $object_type = '' ) {
    
    	if ( $object_type ) {
    		$this->object_type = $object_type;
    		return $this->object_type;
    	}
    		
    	if ( $this->object_type ) {
    		return $this->object_type;
    	}
    	
    	$this->object_type = $this->current_object_type();
    
    	return $this->object_type;
    }

    Also, this is something that doesn’t happen in every project I’ve used CMB2 for terms meta, it only happened in this one.

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘Term meta is saved but it doesn't appear in term edit screen later’ is closed to new replies.