# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: wp-cli, urls, migration
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/cli-list-all-urls/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Export Every Public URL (WP-CLI)" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
