Skip to content

Instantly share code, notes, and snippets.

@jmcausing
Created June 21, 2020 23:16
Show Gist options
  • Save jmcausing/a081541b54194d9d7e4fb7c1fdc8a816 to your computer and use it in GitHub Desktop.
Save jmcausing/a081541b54194d9d7e4fb7c1fdc8a816 to your computer and use it in GitHub Desktop.
loop create post
/// This is your ajax handler called to generate contents file
function wcg_start_generate_function() {
// Retrieve data from ajax
if( isset( $_POST[ "post_type" ] ) ) {
$post_type = $_POST[ "post_type" ];
$quantity = $_POST[ "post_qty" ];
// ==========
// start loop
for($x = 1; $x <= $quantity; $x++) {
$wcg_post_title = Lorem::title(1);
$wcg_post_body = Lorem_body::body(5);
// echo 'This is the post type selected ' . $post_type;
// Gather post data.
$my_post = array(
'post_title' => $wcg_post_title,
'post_type' => $post_type,
'post_content' => $wcg_post_body,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
);
// Insert the post into the database while getting the post_id
$result = wp_insert_post( $my_post );
echo "Post ID " . $result . " inserted!\n";
if ( $result && ! is_wp_error( $result ) ) {
$post_id = $result;
// insert post meta for flagging
update_post_meta($post_id, 'wp_content_generator', '1' );
}
}
// end loop
// ==========
// For debugging
$count_post = $x - 1;
echo "Done! " . $count_post . " post inserted!\n";
die;
}
}
add_action('wp_ajax_wcg_start_generate', 'wcg_start_generate_function'); // wp_ajax_{action}
add_action('wp_ajax_wcg_start_generate', 'wcg_start_generate_function'); // wp_ajax_nopriv_{action}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment