4
votes

I have created a Taxonomy Meta Data for Custom Post Type and its working fine while saving, updating, editing etc. But its not working while importing or exporting with wordpress importer. Taxonomies meta data are not importing.

Here is the Code based on the Tutorial

<?php
if ( ! function_exists( 'register_cpt_houses' ) ) {
    function register_cpt_houses() {
        $labels = array(
            'name'              => __('houses', 'my_plugin'),
            'singular_name'     => __('houses', 'my_plugin'),
            'add_new'           => __('Add New','my_plugin'),
            'add_new_item'      => __('Add New Slide','my_plugin'),
            'edit_item'         => __('Edit houses','my_plugin'),
            'new_item'          => __('New houses','my_plugin'),
            'view_item'         => __('View houses','my_plugin'),
            'search_items'      => __('Search houses','my_plugin'),
            'not_found'         => __('No houses found','my_plugin'),
            'not_found_in_trash'=> __('No houses found in Trash','my_plugin'),
            'parent_item_colon' => '' ,
            'all_items' =>  __( 'All houses' ,'my_plugin')
        );

        $args = array(
            'labels'            => $labels,
            'public'            => true,
            'exclude_from_search'=> false,
            'show_ui'           => true,
            'capability_type'   => 'post',
            'hierarchical'      => false,
            'rewrite'           => array('slug'=>'houses', 'with_front' => true ),
            'query_var'         => false,
            'menu_position'     => null,
            'supports'          => array('title', 'thumbnail', 'page-attributes')
        );

        register_post_type('houses',$args);
    }
    add_action('init', 'register_cpt_houses');
}
function register_feature_taxonomy() {
    $labels = array(
        'name' => _x( 'Features', 'taxonomy general name', 'my_plugin' ),
        'singular_name' => _x('Features', 'taxonomy singular name', 'my_plugin'),
        'search_items' => __('Search Feature', 'my_plugin'),
        'popular_items' => __('Common Features', 'my_plugin'),
        'all_items' => __('All Features', 'my_plugin'),
        'edit_item' => __('Edit Feature', 'my_plugin'),
        'update_item' => __('Update Feature', 'my_plugin'),
        'add_new_item' => __('Add new Feature', 'my_plugin'),
        'new_item_name' => __('New Feature:', 'my_plugin'),
        'add_or_remove_items' => __('Remove Feature', 'my_plugin'),
        'choose_from_most_used' => __('Choose from common Feature', 'my_plugin'),
        'not_found' => __('No Feature found.', 'my_plugin'),
        'menu_name' => __('Features', 'my_plugin'),
    );

    $args = array(
        'hierarchical' => false,
        'labels' => $labels,
        'show_ui' => true,
    );

    register_taxonomy('house_feature','houses', $args);
}
add_action( 'house_feature_add_form_fields', 'add_feature_group_field', 10, 2 );
function add_feature_group_field($taxonomy) {
    global $feature_groups;
    ?><div class="form-field term-group">
        <label for="featuret-group"><?php _e('Feature Group', 'my_plugin'); ?></label>
        <select class="postform" id="equipment-group" name="feature-group">
            <option value="-1"><?php _e('none', 'my_plugin'); ?></option><?php foreach ($feature_groups as $_group_key => $_group) : ?>
                <option value="<?php echo $_group_key; ?>" class=""><?php echo $_group; ?></option>
            <?php endforeach; ?>
        </select>
    </div><?php
}
add_action( 'created_house_feature', 'save_feature_meta', 10, 2 );

function save_feature_meta( $term_id, $tt_id ){
    if( isset( $_POST['feature-group'] ) && '' !== $_POST['feature-group'] ){
        $group = sanitize_title( $_POST['feature-group'] );
        add_term_meta( $term_id, 'feature-group', $group, true );
    }
}
add_action( 'house_feature_edit_form_fields', 'edit_feature_group_field', 10, 2 );

function edit_feature_group_field( $term, $taxonomy ){

    global $feature_groups;

    // get current group
    $feature_group = get_term_meta( $term->term_id, 'feature-group', true );

    ?><tr class="form-field term-group-wrap">
        <th scope="row"><label for="feature-group"><?php _e( 'Feature Group', 'my_plugin' ); ?></label></th>
        <td><select class="postform" id="feature-group" name="feature-group">
            <option value="-1"><?php _e( 'none', 'my_plugin' ); ?></option>
            <?php foreach( $feature_groups as $_group_key => $_group ) : ?>
                <option value="<?php echo $_group_key; ?>" <?php selected( $feature_group, $_group_key ); ?>><?php echo $_group; ?></option>
            <?php endforeach; ?>
        </select></td>
    </tr><?php
}
add_action( 'edited_house_feature', 'update_feature_meta', 10, 2 );

function update_feature_meta( $term_id, $tt_id ){

    if( isset( $_POST['feature-group'] ) && '' !== $_POST['feature-group'] ){
        $group = sanitize_title( $_POST['feature-group'] );
        update_term_meta( $term_id, 'feature-group', $group );
    }
}

add_filter('manage_edit-house_feature_columns', 'add_feature_group_column' );

function add_feature_group_column( $columns ){
    $columns['feature_group'] = __( 'Group', 'my_plugin' );
    return $columns;
}
add_filter('manage_house_feature_custom_column', 'add_feature_group_column_content', 10, 3 );

function add_feature_group_column_content( $content, $column_name, $term_id ){
    global $feature_groups;

    if( $column_name !== 'feature_group' ){
        return $content;
    }

    $term_id = absint( $term_id );
    $feature_group = get_term_meta( $term_id, 'feature-group', true );

    if( !empty( $feature_group ) ){
        $content .= esc_attr( $feature_groups[ $feature_group ] );
    }

    return $content;
}
add_filter( 'manage_edit-house_feature_sortable_columns', 'add_feature_group_column_sortable' );

function add_feature_group_column_sortable( $sortable ){
    $sortable[ 'feature_group' ] = 'feature_group';
    return $sortable;
}
?>

What am I doing wrong?

1
just hazarding a guess, your post is not that clear......get_option( "taxonomy_$t_id"); suggests you are saving your term meta in the options table. Problem is when you export this data, the key name will not change so will remain taxonomy_12_id , where-as your terms will most likely have new ids e.g. 14. So you have a term with a new id of 14 and it is looking for taxonomy_14_id and the actual data is stored in taxonomy_12_idDavid
I have updated the question .Maqk
none the less its using the option table which is not supposed to be used for specific data (i'm not saying its wrong). So when the plugin prepares the export, it will not update the options key because it doesn't know it exists. But wp introduced term_meta in 3.5 i think so google that, it would be a lot easier to use the inbuilt functions ?David
Added the new code based on the term_meta as well but that didn't worked out. I have just updated my codeMaqk
you'll need to be more specificDavid

1 Answers

1
votes

If you need to export the data in termmeta table then try this plugin to export the termmeta data in separate XML file.

Export custom post type will only get you connection between the taxonomy ID and Post exported. To get the Meta data of that taxonomy try this plugin It will provide you a XML containing all additional entries of that Taxonomy you can import that too

https://wordpress.org/plugins/wp-export-categories-taxonomies/

You will find this in Tools -> Wp Export Cats & Taxs

Select the Taxonomy in custom post types you want to export ..