Skip to content

Instantly share code, notes, and snippets.

@ihslimn
Last active October 6, 2022 13:50
Show Gist options
  • Save ihslimn/8bf1b14fb4b8b8f7c2ff43e21f101956 to your computer and use it in GitHub Desktop.
Save ihslimn/8bf1b14fb4b8b8f7c2ff43e21f101956 to your computer and use it in GitHub Desktop.
convert CCT to CPT
<?php
add_action( 'wp_ajax_cycle_through_cct_to_clone', 'cycle_through_cct_to_clone' );
function cycle_through_cct_to_clone() {
$type_object = Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types( 'blum_cct' );
$handler = $type_object->get_item_handler();
$query = Jet_Engine\Query_Builder\Manager::instance()->get_query_by_id( 196 );
$items = $query->get_items();
$flag = \ARRAY_A;
$type_object->db->set_format_flag( $flag );
if ( empty( $items ) ) {
wp_send_json( array( 'repeat' => false ) );
}
$start = microtime( true );
foreach ( $items as $item ) {
$item_array = get_object_vars( $item );
$title = 'clone_' . $item_array['_ID'];
$status = $item_array['cct_status'];
$author = $item_array['cct_author_id'];
$meta_input = $item_array;
unset( $meta_input['_ID'] );
unset( $meta_input['cct_status'] );
unset( $meta_input['cct_author_id'] );
unset( $meta_input['cct_single_post_id'] );
unset( $meta_input['cloned_post'] );
unset( $meta_input['cct_slug'] );
$post_id = wp_insert_post(
array(
'post_title' => $title,
'post_author' => $author,
'post_status' => $status,
'post_type' => 'blum_cct_clone',
'meta_input' => $meta_input
)
);
if ( $post_id ) {
$item_array['cloned_post'] = $post_id;
$handler->update_item( $item_array );
}
if ( microtime( true ) - $start > 30 ) {
wp_send_json( array( 'repeat' => true ) );
}
}
wp_send_json( array( 'repeat' => false ) );
}
<button id="create-cct-single">CREATE</button>
<script>
( function( $ ) {
$( document ).ready( function() {
$( '#create-cct-single' ).click( recreateSingles );
function recreateSingles() {
$.ajax( {
url: JetEngineSettings.ajaxurl,
type: 'POST',
dataType: 'json',
data : {
action : 'cycle_through_cct_to_clone',
},
} ).done( function( response ) {
if ( response.repeat ) {
recreateSingles();
} else {
alert( 'Done!' );
}
} );
}
} );
}( jQuery ) );
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment