wp_insert_post generates endless posts

I’m using wp_insert_posts to programmatically create a post but it keeps generating the same post endlessly. I’m using a custom post type called irl-today. It does generate one post if I’m using the page custom post type but does an endless generate of the post on the ‘irl-today` custom post type.

Is there a way to generate a post only once? This is what I have in my functions.php:

<?php
if ( !is_admin() ) { /* Main site*/
} else { /* Admin */

if( ! get_page_by_title(‘Archive’) ) {
$my_post = array( ‘post_title’ => ‘Archive’,
‘import_id’ => ‘1041’,
‘post_type’ => ‘irl-today’,
‘post_name’ => ‘archives’,
‘post_content’ => ‘All of DailyBayou publications.’,
‘post_status’ => ‘publish’,
‘comment_status’ => ‘closed’,
‘ping_status’ => ‘closed’,
‘post_author’ => 1,
‘menu_order’ => 0,
‘guid’ => site_url() . ‘/archives’ );
$PageID = wp_insert_post( $my_post, FALSE ); // Get Post ID – FALSE to return 0 instead of wp_error.
}
};?>

It does generate infinity posts even if it’s outside the if { } else { };

Read more here:: wp_insert_post generates endless posts

Leave a Reply

Your email address will not be published. Required fields are marked *