Skip to content

Instantly share code, notes, and snippets.

@isotrope
Created September 23, 2014 14:39
Show Gist options
  • Save isotrope/1b9b1f991c63961f8696 to your computer and use it in GitHub Desktop.
Save isotrope/1b9b1f991c63961f8696 to your computer and use it in GitHub Desktop.
Exporting a CPT with its Custom Taxonomies
<?php
/**
* Created by PhpStorm.
* User: Michal
* Date: 2014-09-23
* Time: 10:28 AM
*/
require( '../../../../../wp-load.php' );
$args = array(
'post_status' => 'publish',
'post_type' => 'your_cpt',
'posts_per_page' => 9999,
);
$all_query = new WP_Query( $args );
$everything = [ ];
if ( $all_query->have_posts() ) {
while ( $all_query->have_posts() ) {
$all_query->the_post();
// Get the taxonomies you need
$tax1 = get_the_terms(get_the_ID(), 'your_custom_taxonomy1');
$tax2 = get_the_terms(get_the_ID(), 'your_custom_taxonomy2');
// Chances are that you simply want the Name
$tax1_terms = implode( ', ', wp_list_pluck( $tax1, 'name' ) );
$tax2_terms = implode( ', ', wp_list_pluck( $tax2, 'name' ) );
// This part.. I don't remember the proper way to simply pass the $post
global $post;
$post_info['post'] = $post;
$post_into['tax1'] = $tax1_terms;
$post_into['tax2'] = $tax2_terms;
$everything[] = $post_info;
}
$fp = fopen('data.json', 'w');
fwrite($fp, json_encode($everything));
fclose($fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment