Using WordPress ‘export_wp()’ PHP function

The export_wp() WordPress PHP function generates the WXR (WordPress eXtended RSS) export file for download. It exports all content by default, excluding posts with ‘auto-draft’ status. You can customize the export by defining specific parameters.

Usage

Here’s a basic usage example of export_wp() function:

$args = array(
  'content' => 'post',
  'author' => '1',
);
export_wp($args);

In this example, the function exports all posts (‘content’ => ‘post’) written by the author with ID 1 (‘author’ => ‘1’).

Parameters

  • $args (array) – Optional. Arguments for generating the WXR export file for download. Here are the options you can include in the array:
    • ‘content’ (string) – Type of content to export. Defaults to ‘all’.
    • ‘author’ (string) – Author to export content for. Defaults to false (all).
    • ‘category’ (string) – Category to export content for. Used only when ‘content’ is ‘post’. Defaults to false (all categories).
    • ‘start_date’ (string) – Start date to export content from. Expected date format is ‘Y-m-d’. Defaults to false (from the beginning).
    • ‘end_date’ (string) – End date to export content to. Expected date format is ‘Y-m-d’. Defaults to false (latest publish date).
    • ‘status’ (string) – Post status to export posts for. Defaults to false (all statuses except ‘auto-draft’).

More information

See WordPress Developer Resources: export_wp()

Examples

Export all content

The function without any arguments will export all content:

export_wp();

Export specific post type

To export only posts:

$args = array('content' => 'post');
export_wp($args);

Export posts from a specific author

To export posts from author with ID 1:

$args = array('content' => 'post', 'author' => '1');
export_wp($args);

Export posts from a specific category

To export posts from the category ‘news’:

$args = array('content' => 'post', 'category' => 'news');
export_wp($args);

Export posts within a specific date range

To export posts published between ‘2023-01-01’ and ‘2023-12-31’:

$args = array('content' => 'post', 'start_date' => '2023-01-01', 'end_date' => '2023-12-31');
export_wp($args);