This’ll output a CSV export of:
- Post title
- Post slug
- Post content
- Order ID
- Category ID
Pop this in a file in the root of your WordPress install (e.g. export.php
), make sure you change the $category_ids
with the IDs of all the specific categories.
<?php
require './wp-load.php';
header( 'Content-Type: text/csv; charset=" . get_bloginfo( "charset' ) );
header( 'Content-Disposition: attachment; filename=posts.csv' );
$category_tax = 'category'; // Change if different category taxonomy
$category_ids = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; // The 8 specific category IDs
$query = new WP_Query;
$paged = 1;
$fopen = fopen( 'php://output', 'w' );
while ( true ) {
$query->query([
'post_type' => 'post', // Change if different post type
'post_status' => [ 'publish', 'private' ],
'posts_per_page' => 50,
'no_found_rows' => true,
'paged' => $paged++,
'tax_query' => [[
'taxonomy' => $category_tax,
'terms' => $category_ids,
]],
]);
if ( ! $query->have_posts() )
break;
foreach ( $query->posts as $post ) {
$cats = [];
if ( $terms = get_the_terms( $post, $category_tax ) ) {
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) )
$cats[] = $term->term_id;
}
}
fputcsv( $fopen, [
$post->post_title,
$post->post_name, // Slug - use get_page_uri( $post ) instead if posts are hierarchical
$post->post_content, // Use apply_filters( 'the_content', $post->post_content ) if you want HTML-rendered frontend output
$post->menu_order, // Order ID
implode( ':', $cats ), // Colon-separated list of the category IDs (if more than one)
]);
}
}