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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.