• Resolved mikevisions

    (@mikevisions)


    I have added custom fields to the terms in a custom taxonomy.
    All saving correctly to the termmeta table but am having trouble getting the data to display on the front end.
    This is the code I am using;

    $mdmeta = get_term_meta( $term_id, ‘wgmdt_term_show_free’ );

    echo $mdmeta ;
    The key is correct as in the database table. Any help would be appreciated,
    Mike.

    https://ww.wp.xz.cn/plugins/cmb2/

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

    (@tw2113)

    The BenchPresser

    I would end up doing a var_dump() on the $mdmeta variable to see if it’s returning anything at all. Even an empty string. Possible you’re not getting results back like you’re thinking.

    Are you sure $term_id has the correct term ID value that you’re expecting?

    Thread Starter mikevisions

    (@mikevisions)

    Hi Michael,
    var_dump gives me bool(false)
    If I echo the term id it prints out correctly.
    Removing the custom field key so that all meta data prints out also gives me bool(false)
    I am at a loss to understand what is going on.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    sounds like the function isn’t finding what’s expected.

    Can you provide all the code samples you’re using for this? Including the fields and what you’re using to try and display the data?

    Wanting to see if I can manage to recreate the results. Also may show a missing detail as to what’s going on, always a possibility.

    Thread Starter mikevisions

    (@mikevisions)

    Hi Michael,
    Thanks for your offer of help.
    I am using the get term meta from the codex. It is also the same format that I have working for custom post meta. I am using it in a custom taxonomy but have tried it in a standard category and got the same result so I don’t think it is anything to do with custom taxonomy or custom post type.
    ‘wgmdt_term_ is the prefix and show_free is the key for the custom field. I have confirmed that this is being saved in the termmeta table.

    $mdmeta = get_term_meta( term_id, 'wgmdt_term_show_free' );
    echo    ' <li> '.$mdmeta. ' </li> ';

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you provide the CMB2 field code as well, so I can see how the fields are being added and see how it’s all getting to the database?

    Thread Starter mikevisions

    (@mikevisions)

    This is the cmb2 code for generating the custom field

    add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' );
    /**
     * Hook in and add a metabox to add fields to taxonomy terms
     */
    function yourprefix_register_taxonomy_metabox() {
    	$prefix = 'wgmdt_term_';
    
    	/**
    	 * Metabox to add fields to categories and tags
    	 */
    	$cmb_term = new_cmb2_box( array(
    		'id'               => $prefix . 'edit',
    		'title'            => __( 'Category Metabox', 'cmb2' ), // Doesn't output for term boxes
    		'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
    		'taxonomies'       => array( 'supplier_categories' ), // Tells CMB2 which taxonomies should have these fields
    		// 'new_term_section' => true, // Will display in the "Add New Category" section
    	) );
    	$cmb_term->add_field( array(
    		'name' => __( 'Show free entries', 'cmb2' ),
    		'desc' => __( 'field description (optional)', 'cmb2' ),
    		'id'   => $prefix . 'show_free',
    		'type'             => 'select',
        'show_option_none' => false,
        'options'          => array(
    	    'yes'   => __( 'Yes', 'cmb2' ),
            'no'   => __( 'No', 'cmb2' ),
    
          ),
    	) );
    }

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Biggest thing I can see is that get_term_meta() is returning an array with 1 index that holes the value from your dropdown.

    echo $mdmeta[0]

    Above should work.

    Thread Starter mikevisions

    (@mikevisions)

    Sadly that still does not work.
    Using var dump returns NULL
    I have tried this with the default CMB2 code for a text field and that does not display either.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Something else must be going on then that’s causing the null return value. I used the code above to set things up and it was working for me. However, we would have different installs with different things going on as well, which can cause different results.

    Thread Starter mikevisions

    (@mikevisions)

    I have just done a clean wordpress install on localhost and used the default CMB2 code and get the same result with a NULL return

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Wish I had more to tell ya. I am not sure why it’s returning null on you. The get_term_meta() function isn’t supposed to return that, it’ll return false or empty string or so.

    Just to cover some bases, you are on 4.4.x correct?

    Can you screenshot a view of the term meta table and provide the term ID you’re using here? Perhaps somehow “null” got saved to the spot.

    https://developer.ww.wp.xz.cn/reference/functions/get_term_meta/

    Thread Starter mikevisions

    (@mikevisions)

    Solved it.

    $mdmeta = get_term_meta( $term->term_id, 'wgmdt_term_show_free' );
       echo    ' <li> '.$mdmeta[0]. ' </li> ';

    Thanks for all your help.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Awesome to hear Mike. Glad to know you’re on your way.

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘Displaying term meta data’ is closed to new replies.