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


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 yourdomain.com/get-urls.php

Get List of Attachments

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

<?php
include "wp-load.php";
?>

<?php query_posts($args); ?>
    <?php while (have_posts()) : the_post(); ?>
        <?php
        // https://codex.wordpress.org/Function_Reference/get_allowed_mime_types
        $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

<?php 
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(
        'post',
        'page'
    ),
    '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;
header('Content-type:text/plain');


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


foreach($posts as $post) {

    switch ($post->post_type) {
        case 'revision':
        case 'nav_menu_item':
            break;
        case 'page':
            $permalink = get_page_link($post->ID);
            break;
        case 'post':
            $permalink = get_permalink($post->ID);
            break;
        case 'attachment':
            $permalink = get_attachment_link($post->ID);
            break;
        default:
            $permalink = get_post_permalink($post->ID);
            break;
    }
    $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

(https:\/\/www.yourdomain.com\/wp-content\/uploads\/)\d{4}\/\d{2}\/

 


About the Author

Jacob Lett (Jake) is the founder of Bootstrap Creative, a B2B marketing consultancy specializing in manufacturers and industrial companies. A HubSpot-certified CMS developer, SEO, Answer Engine Optimization (AEO), and RevOps professional, he helps B2B companies improve their websites, search visibility, HubSpot systems, and Google Ads campaigns to generate more qualified leads and RFQs. With over a decade of hands-on experience, he acts as a direct partner for companies seeking measurable ROI from their marketing investment.



Related posts
Category: Code Snippets

Tags: ,

| Read My Editorial Policy

Want to Get Email Updates of New Articles?

Join My Email Newsletter