wppaste
WordPress

export_wp( array $args = array() )

Since
2.1.0, 5.7.0
Source
wp-admin/includes/export.php:57
Generates the WXR export file for download.

Description

Default behavior is to export all content, however, note that post content will only be exported for post types with the can_export argument enabled. Any posts with the 'auto-draft' status will be skipped.

Compatibility

WordPress
since 5.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$argsarrayoptional
Arguments for generating the WXR export file for download. Default empty array.Default: array()
  • $contentstringdefault: 'all'

    Type of content to export. If set, only the post content of this post type will be exported. Accepts 'all', 'post', 'page', 'attachment', or a defined custom post. If an invalid custom post type is supplied, every post type for which can_export is enabled will be exported instead. If a valid custom post type is supplied but can_export is disabled, then 'posts' will be exported instead. When 'all' is supplied, only post types with can_export enabled will be exported.
  • $authorstringdefault: false (all)

    Author to export content for. Only used when $content is 'post', 'page', or 'attachment'. Accepts false (all) or a specific author ID.
  • $categorystringdefault: is false (all categories)

    Category (slug) to export content for. Used only when $content is 'post'. If set, only post content assigned to $category will be exported. Accepts false or a specific category slug.
  • $start_datestringdefault: false (since the beginning of time)

    Start date to export content from. Expected date format is 'Y-m-d'. Used only when $content is 'post', 'page' or 'attachment'.
  • $end_datestringdefault: false (latest publish date)

    End date to export content to. Expected date format is 'Y-m-d'. Used only when $content is 'post', 'page' or 'attachment'.
  • $statusstringdefault: false (all statuses except 'auto-draft')

    Post status to export posts for. Used only when $content is 'post' or 'page'. Accepts false (all statuses except 'auto-draft'), or a specific status, i.e. 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', or 'trash'.

Performance profile

How much work a call to export_wp() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_col().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
202–253

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 973.

Plugin surface
8 hooks

Third-party callbacks on 'export_wp', 'export_wp_filename', 'rss2_head' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • optionoption read or writeget_option()called directly
  • querycontent queryget_term()called directly
  • sqldatabase query->get_col()called directly
  • cacheobject cachewp_cache_get()one call below export_wp()
  • serializeserialisationmaybe_unserialize()one call below export_wp()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost export_wp() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($sitename) && !$args202–206wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), do_action()
empty($sitename) && !$args204–208wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), wxr_nav_menu_terms(), do_action()
empty($sitename) && !$args213–217wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), do_action(), array_splice()
empty($sitename) && !$args && isset($term)213–214wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), get_term(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), do_action()
empty($sitename) && !$args215–219wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), wxr_nav_menu_terms(), do_action(), array_splice()
empty($sitename) && !$args && isset($term)215–216wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), get_term(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), wxr_nav_menu_terms(), do_action()
empty($sitename) && !$args && isset($term)224–225wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), get_term(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), do_action(), array_splice()
empty($sitename) && !$args && isset($term)226wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), get_term(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), wxr_nav_menu_terms(), do_action(), array_splice()
empty($sitename) && !$args236–240wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), get_categories(), get_tags(), get_taxonomies(), taxonomy(), array_shift(), array_shift(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), do_action()
empty($sitename) && !$args238–242wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), get_categories(), get_tags(), get_taxonomies(), taxonomy(), array_shift(), array_shift(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), wxr_nav_menu_terms(), do_action()
empty($sitename) && !$args247–251wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), get_categories(), get_tags(), get_taxonomies(), taxonomy(), array_shift(), array_shift(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), do_action(), array_splice()
empty($sitename) && !$args249–253wp_parse_args(), do_action(), get_bloginfo(), sanitize_key(), gmdate(), apply_filters(), header(), header(), get_option(), header(), get_post_types(), array_fill(), ->prepare(), ->get_col(), get_categories(), get_tags(), get_taxonomies(), taxonomy(), array_shift(), array_shift(), add_filter(), get_bloginfo(), the_generator(), bloginfo_rss(), bloginfo_rss(), bloginfo_rss(), gmdate(), bloginfo_rss(), wxr_site_url(), bloginfo_rss(), wxr_authors_list(), wxr_nav_menu_terms(), do_action(), array_splice()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev973202–25348
8.5973202–25348
8.4973202–253489 fewer instructions than PHP 8.3
8.3982205–25648
8.2982205–256481 more instruction than PHP 8.1
8.1981205–261482 fewer instructions than PHP 7.4
7.4983205–26148

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 8

8 hooks fire while export_wp() runs, in this order:

  1. do_action( export_wp )actionline 77 (+20 into the body)

    Fires at the beginning of an export, before any headers are sent.

  2. apply_filters( export_wp_filename )filterline 94 (+37 into the body)

    Filters the export filename.

  3. do_action( rss2_head )actionline 576 (+519 into the body)

    Fires at the end of the RSS2 Feed Header.

  4. apply_filters( the_title_export )filterline 605 (+548 into the body)

    Filters the post title used for WXR exports.

  5. apply_filters( the_content_export )filterline 614 (+557 into the body)

    Filters the post content used for WXR exports.

  6. apply_filters( the_excerpt_export )filterline 623 (+566 into the body)

    Filters the post excerpt used for WXR exports.

  7. apply_filters( wxr_export_skip_postmeta )filterline 669 (+612 into the body)

    Filters whether to selectively skip post meta used for WXR exports.

  8. apply_filters( wxr_export_skip_commentmeta )filterline 712 (+655 into the body)

    Filters whether to selectively skip comment meta used for WXR exports.

Uses · 39

Show all 39

Source code

function export_wp( $args = array() ) {	global $wpdb, $post; 	$defaults = array(		'content'    => 'all',		'author'     => false,		'category'   => false,		'start_date' => false,		'end_date'   => false,		'status'     => false,	);	$args     = wp_parse_args( $args, $defaults ); 	/**	 * Fires at the beginning of an export, before any headers are sent.	 *	 * @since 2.3.0	 *	 * @param array $args An array of export arguments.	 */	do_action( 'export_wp', $args ); 	$sitename = sanitize_key( get_bloginfo( 'name' ) );	if ( ! empty( $sitename ) ) {		$sitename .= '.';	}	$date        = gmdate( 'Y-m-d' );	$wp_filename = $sitename . 'WordPress.' . $date . '.xml';	/**	 * Filters the export filename.	 *	 * @since 4.4.0	 *	 * @param string $wp_filename The name of the file for download.	 * @param string $sitename    The site name.	 * @param string $date        Today's date, formatted.	 */	$filename = apply_filters( 'export_wp_filename', $wp_filename, $sitename, $date ); 	header( 'Content-Description: File Transfer' );	header( 'Content-Disposition: attachment; filename=' . $filename );	header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); 	if ( 'all' !== $args['content'] && post_type_exists( $args['content'] ) ) {		$ptype = get_post_type_object( $args['content'] );		if ( ! $ptype->can_export ) {			$args['content'] = 'post';		} 		$where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );	} else {		$post_types = get_post_types( array( 'can_export' => true ) );		$esses      = array_fill( 0, count( $post_types ), '%s' ); 		// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare		$where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );	} 	if ( $args['status'] && ( 'post' === $args['content'] || 'page' === $args['content'] ) ) {		$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );	} else {		$where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";	} 	$join = '';	if ( $args['category'] && 'post' === $args['content'] ) {		$term = term_exists( $args['category'], 'category' );		if ( $term ) {			$join   = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";			$where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );		}	} 	if ( in_array( $args['content'], array( 'post', 'page', 'attachment' ), true ) ) {		if ( $args['author'] ) {			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );		} 		if ( $args['start_date'] ) {			$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", gmdate( 'Y-m-d', strtotime( $args['start_date'] ) ) );

Changelog

Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.7.0
Added the post_modified and post_modified_gmt fields to the export file.from the docblock
2.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/export.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.