Paulund
2016-12-08 #wordpress

Export All WordPress URLs

In this tutorial we're going to create a way you can export all of your WordPress URLs into a CSV file. This is very important task to do when migrating a website as you'll be able to go through each of your URLs and make sure that they can still work on your new website. If your URLs don't work on your new site then you'll need to make sure that you can redirect those old URLs to the new ones.

There's two stages to this tutorial, first we need to get all the posts/pages out of WordPress and grab their URLs. Next we need to take these posts/pages and output these into a CSV.

Get All Posts And Pages

To get all the posts and pages in WordPress you can use the WP_Query object, this takes a number of arguments to help you get the posts you need. The options that we need are:

  • post_type - Post types to fetch
  • post_status - Post statuses to fetch
  • posts_per_page - Amount of posts to fetch

The post types that we need are anything that is publicly accessible such as posts, pages and attachments. We can access any of the other custom post types by using the function get_post_types.

$postTypes = get_post_types([
    'public' => true,
    'exclude_from_search' => false
]);

You can use the variable $postTypes as the array for the value in WP_Query. The post_status value will need to be set to publish as we only want to retrieve live posts. To get all of the posts in your website we'll need to use the parameter posts_per_page with the value of -1.

function getAllPublishedUrls()
{
    $postTypes = get_post_types([
        'public' => true,
        'exclude_from_search' => false
    ]);

    $query = new \WP_Query([
        'post_type' => array_values($postTypes),
        'post_status' => 'publish',
        'posts_per_page'    => -1,
    ]);

    return $query->get_posts();
}

The above function will return all the posts and pages within our WordPress site in an array. ## Convert Post Array To CSV

To create the CSV which will automatically download we need to take the post array and convert this to a CSV. Using PHP it's very easy to do this by using the function fputcsv. Using the following code we can take the posts array create the array for the results consisting of the post title and the post URL and add these to a CSV file using fputcsv.

$posts = $this->getAllPublishedUrls();

$resultsArray = array();

// loop over the input array
foreach ($posts as $index => $post)
{
    $resultsArray[$index]['title'] = $post->post_title;
    $resultsArray[$index]['url'] = get_the_permalink($post->ID);
}

$f = fopen('php://memory', 'w');
fputcsv($f, array('title','url'));

foreach($resultsArray as $array) {
    fputcsv($f, array_values($array));
}

fseek($f, 0);

header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="export.csv";');

fpassthru($f);

exit;

UpChecker

Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring