Halloween Sale 15% off Ends in See the plans
PHP

Export Every Public URL (WP-CLI)

Adds a "wp list-urls" command that prints the address of every published post, page, custom post and term archive, ready to build a redirect map before a migration or redesign.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
    return;
}

WP_CLI::add_command( 'list-urls', function ( $args, $assoc_args ) {
    $items = array( array( 'type' => 'home', 'id' => 0, 'url' => home_url( '/' ) ) );

    foreach ( get_post_types( array( 'public' => true ) ) as $post_type ) {
        if ( 'attachment' === $post_type ) {
            continue;
        }
        $paged = 1;
        do {
            $ids = get_posts( array(
                'post_type'      => $post_type,
                'post_status'    => 'publish',
                'posts_per_page' => 500,
                'paged'          => $paged++,
                'fields'         => 'ids',
                'no_found_rows'  => true,
                'orderby'        => 'ID',
                'order'          => 'ASC',
            ) );
            foreach ( $ids as $id ) {
                $items[] = array( 'type' => $post_type, 'id' => $id, 'url' => get_permalink( $id ) );
            }
        } while ( count( $ids ) === 500 );
    }

    if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'terms', true ) ) {
        foreach ( get_taxonomies( array( 'public' => true ) ) as $taxonomy ) {
            foreach ( get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => true ) ) as $term ) {
                $items[] = array( 'type' => $taxonomy, 'id' => $term->term_id, 'url' => get_term_link( $term ) );
            }
        }
    }

    WP_CLI\Utils\format_items( $assoc_args['format'] ?? 'table', $items, array( 'type', 'id', 'url' ) );
}, array(
    'shortdesc' => 'Lists the URL of every published item.',
    'synopsis'  => array(
        array( 'type' => 'flag', 'name' => 'terms', 'optional' => true, 'default' => true, 'description' => 'Include term archives. Use --no-terms to skip.' ),
        array( 'type' => 'assoc', 'name' => 'format', 'optional' => true, 'default' => 'table', 'options' => array( 'table', 'csv', 'json' ) ),
    ),
) );

Notes

Save the list with "wp list-urls --format=csv > urls.csv"; add --no-terms to skip category and tag archives.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.