nancylee973
nancylee973

Reputation: 3

wp_insert_post creates duplicate posts every time the page loads

the code below runs and create duplicate posts (sometimes in batches of 2 and 4 duplicate posts) every time the page loads even though I already checked if the title already exists ...

//checking to see if the title already exists.
if( null == get_page_by_title( $title, 'OBJECT', 'post' ) ) {
// Create post object
$my_post = array(
  'post_title'    => 'My post',
  'post_content'  => 'This is my post.',
  'post_status'   => 'publish'
);

// Insert the post into the database
wp_insert_post( $my_post );
}

any way I can prevent this behavior so I can time and schedule the blog posting?

Upvotes: 0

Views: 1730

Answers (1)

ffflabs
ffflabs

Reputation: 17491

The second parameter for get_page_by_title should be a constant. Not a string.

if( null == get_page_by_title( $title, OBJECT, 'post' ) ) {
...
}

Let's discard that before digging deeper.

Upvotes: 1

Related Questions