How to Get a List of WordPress PDF, Image, Post, or Page URLS

Download FREE Marketing Guides

Below are two PHP scripts you can use to help get a list of URLs of content in your WordPress site. I use these when I am migrating posts or media library files to another sites without using a plugin.

Follow the steps below to use these scripts

  1. Open a text editor and copy and paste one of the code blocks and save the file as get-urls.php
  2. Upload the get-urls.php file to the root directory of your wordpress install not your themes folder
  3. In your browser go to

Get List of Attachments

Once you have your list of URLS you can use this handy chrome extension to download them into a folder.

include "wp-load.php";

<?php query_posts($args); ?>
    <?php while (have_posts()) : the_post(); ?>
        $unsupported_mimes  = array('image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon', 'application/pdf', 'video/mp4');
        $all_mimes          = get_allowed_mime_types();
        $accepted_mimes     = array_diff($all_mimes, $unsupported_mimes);

        $args = array(
            'post_mime_type'    => $accepted_mimes,
            'order'          => 'ASC',
            'orderby'          => 'date',
            'post_type'      => 'attachment',
              'post_parent'    => $post->ID,
              'date_query'     => array(
                'year'  => 2016,
                'before' => array(
               'year'  => 2018,
              'month' => 1,
              'day'   => 1,
            //'post_status'    => null,
            'numberposts'    => -1,
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ($attachments as $attachment) {
                echo wp_get_attachment_url($attachment->ID);
                echo "<br>";
    <?php endwhile; ?>


Get List of Pages

include "wp-load.php";

// Get All Post Types as List
foreach ( get_post_types( '', 'names' ) as $post_type ) {
    echo "{$post_type}\n";
    //echo $post_type ."<br>";

// Step 1. Paste all of the post types into this array below
// Step 2. Save page as .csv and remove the post types at the top

$posts = new WP_Query(array(
    'post_type' => array(
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'depth' => -1

//$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish&depth=-1');
$posts = $posts->posts;

echo "URL,";
echo "Type,";
echo "Date,";
echo "Title\n";

foreach($posts as $post) {

    switch ($post->post_type) {
        case 'revision':
        case 'nav_menu_item':
        case 'page':
            $permalink = get_page_link($post->ID);
        case 'post':
            $permalink = get_permalink($post->ID);
        case 'attachment':
            $permalink = get_attachment_link($post->ID);
            $permalink = get_post_permalink($post->ID);
    $title = get_the_title($post->ID);
    $postType = get_post_type($post->ID);
    $postDate = get_the_date('Y-m-d');
    echo "{$permalink},";
    echo "{$postType},";
    echo "{$postDate},";
    echo "{$title}\n";

Regex Find and Replace Old domain with New One



Written by: Jake Lett
I share digital marketing tips and HubSpot tutorials to help marketers and business owners grow their business.

Related posts

Tags: ,

Want to Get Email Updates of New Articles?

Join My Email Newsletter