WordPress Wp_insert_post() not working no errors after duplicates check

0

I have created a plugin to populate custom posts from json data, the first two posts populate without error but third fails and I think it is when it gets to the check to see if the post already exists when no posts of this type exist.
For the exists check I copied the post_exists() function and added it to my functions php file and called it custom_exists()

the code I have for post creation is as follows:

 foreach($team_data as $teamitem)
{

 $teamtitle = $teamitem->name;
 $teamID = $teamitem->id;

if(custom_exists($teamtitle) == 0)
{
 $team = array(
'meta_query' => array(

    array(
         'key' =>'team_id',
         'value' => $teamID
    )
),

'post_type' => 'teams',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'),
 'posts_per_page' => 1
);

 $teamposts = get_posts($team);
$team_id = '';
if($teamposts)
$team_id = $teamID;
$team_content = '<h2>Teams in this Competition</h2>';

$team_post = array
(
 'ID' => $team_id,
'post_title' => $teamtitle, 
'post_type' => 'team',
'post_author' => 1,
'post_content' => $team_content,
'tags_input' => $teamCompetition,
'post_status' => ($teamposts) ? $teamposts[0] -> post_status : 'publish'

);



$team_id = wp_insert_post($team_post);
update_post_meta( $team_id, '_wp_page_template', 'team.php' );

I have done various debugs and I know that the $teamtitle and $teamID return values but when I try to debug after the if(custom_exists) I can’t get anything to return. Does anyone know why the custom_exists would work for the first two posts but not the third?

Link: WordPress Wp_insert_post() not working no errors after duplicates check
Source: Stack WordPress

Share.

About Author

Leave A Reply