Skip to content

wp export is not exporting term meta data #42

Closed

Description

@sudar

The wp export command is not exporting term meta data.

But the WXR file created directly from wp-admin ui (Tools -> Export) includes the term meta data.

Steps to reproduce

  • Add term meta data to a specific term using either the update_term_meta or add_term_meta function
  • Verify that this data is present in wp_termmeta table
  • Export the contents of the site by running the wp export command
  • The exported WXR file will not have this term meta data. This can be confirmed by doing a grep for wp:termmeta
  • Alternatively export from wp-admin ui by going to Tools -> Export
  • The exported WXR file will contain the term meta data. This can be confirmed by doing a grep for wp:termmeta

Activity

  1. added
    command:export
    on Nov 16, 2018
  2. LittleSnake42 commented on Jun 5, 2019

    @LittleSnake42

    Up please ;)

    I'm gonna try to quick fix it for now.

    WP does it in includes/export.php on line 312

    Here's the code :

    /**
    	 * Output term meta XML tags for a given term object.
    	 *
    	 * @since 4.6.0
    	 *
    	 * @param WP_Term $term Term object.
    	 */
    	function wxr_term_meta( $term ) {
    		global $wpdb;
    
    		$termmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->termmeta WHERE term_id = %d", $term->term_id ) );
    
    		foreach ( $termmeta as $meta ) {
    			/**
    			 * Filters whether to selectively skip term meta used for WXR exports.
    			 *
    			 * Returning a truthy value to the filter will skip the current meta
    			 * object from being exported.
    			 *
    			 * @since 4.6.0
    			 *
    			 * @param bool   $skip     Whether to skip the current piece of term meta. Default false.
    			 * @param string $meta_key Current meta key.
    			 * @param object $meta     Current meta object.
    			 */
    			if ( ! apply_filters( 'wxr_export_skip_termmeta', false, $meta->meta_key, $meta ) ) {
    				printf( "\t\t<wp:termmeta>\n\t\t\t<wp:meta_key>%s</wp:meta_key>\n\t\t\t<wp:meta_value>%s</wp:meta_value>\n\t\t</wp:termmeta>\n", wxr_cdata( $meta->meta_key ), wxr_cdata( $meta->meta_value ) );
    			}
    		}
    	}
    
  3. LittleSnake42 commented on Jun 5, 2019

    @LittleSnake42

    Edit.
    I added the following function (same as comment_meta()) :

    protected function term_meta($term)
    	{
    		global $wpdb;
    		$termmeta = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->termmeta WHERE term_id = %d", $term->term_id));
    		if (!$termmeta) {
    			return new Oxymel();
    		}
    		$oxymel = new WP_Export_Oxymel_Snake();
    		foreach ($termmeta as $meta) {
    			$oxymel->tag('wp:termmeta')->contains
    				->tag('wp:meta_key', $meta->meta_key)
    				->tag('wp:meta_value')->contains->cdata($meta->meta_value)->end
    				->end;
    		}
    		return $oxymel;
    	}
    

    And it is called here :

    ->oxymel($this->term_meta($term));

    
    	protected function terms($terms)
    	{
    		$oxymel = new WP_Export_Oxymel_Snake();
    		foreach ($terms as $term) {
    			$term->parent_slug = $term->parent ? $terms[$term->parent]->slug : '';
    			$oxymel->tag('wp:term')->contains
    				->tag('wp:term_id', $term->term_id)
    				->tag('wp:term_taxonomy', $term->taxonomy)
    				->tag('wp:term_slug', $term->slug)
    				->oxymel($this->term_meta($term));
    
    			if ('nav_menu' !== $term->taxonomy) {
    				$oxymel
    					->tag('wp:term_parent', $term->parent_slug);
    			}
    
    			$oxymel
    				->optional_cdata('wp:term_name', $term->name)
    				->optional_cdata('wp:term_description', $term->description)
    				->end;
    		}
    		return $oxymel->to_string();
    	}
    
  4. Kerfred commented on Jun 13, 2024

    @Kerfred

    I take this issue.

  5. Kerfred commented on Jun 13, 2024

    @Kerfred

    I have done the test manually with success.
    I have also tested successfully the import of the exported XML file.

    The command composer behat doesn't work on my computer. It always returns this error ERROR 1045 (28000): Access denied for user 'wp_cli_test'@'localhost' (using password: YES)
    So I could not run the automatic tests.

  6. Kerfred commented on Jun 5, 2025

    @Kerfred

    composer behat works well now on my computer.
    I have added a scenario and 2 tests.
    All the tests are successful.

  7. linked a pull request that will close this issueInclude term meta in export file #114on Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcommand:exportcontributor-daygood-first-issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions