WP_Insert_Post creating duplicate posts when logged in
I'm creating posts to a custom post type with data in a json URL. I am using the wp_ajax_nopriv action and noticed that while logged in and running the action I get duplicate posts. If I use another browser and stay logged out I get the correct number of posts. I can't figure out what in my code is causing the posts to duplicate.
My function.php page:
add_action('wp_ajax_nopriv_get_breweries_from_api', 'get_breweries_from_api');
add_action('wp_ajax_get_breweries_from_api', 'get_breweries_from_api');
function get_breweries_from_api() {
$ogURL = 'https://api.bridgedataoutput.com/api/v2/OData/actris/Property/replication?access_token=HIDDEN$select=Latitude,Longitude,StreetNumber,StreetName,City,StandardStatus,StateOrProvince,PostalCode,Media,ListPrice,ListingId$unselect=ListingKey,Media.MimeType,Media.ResourceRecordKey,Media.ResourceName,Media.ClassName,Media.MediaCategory,Media.MediaObjectID,Media.MediaKey,Media.Order,Media.ShortDescription$filter=contains(ListingId,%27888%27)';
$current_page = ( ! empty($_POST['nextLink']) ) ? $_POST['nextLink'] : $ogURL;
$activeurl = wp_remote_retrieve_body(wp_remote_get($current_page));
$tet = json_decode($activeurl,true);
$searchresults = $tet['@odata.nextLink'];
$activeurl = json_decode($activeurl);
$activeurl = $activeurl-value;
//quit when empty or not array
if( ! is_array( $activeurl ) || empty ( $activeurl ) ){
return false;
}//end if statement
foreach( $activeurl as $brewery) {
$brewery_slug = sanitize_title($brewery-StreetName . '-' . $brewery-ListingKey);
$inserted_brewery = wp_insert_post([
'post_name' = $brewery_slug,
'post_title' = $brewery_slug,
'post_type' = 'brewery',
'post_status' = 'publish'
]); //end inserted brewery
$fillable = [
'field_619b2f717de72' = 'ListingId',
'field_619b2f987de73' = 'StreetName',
'field_619d285a4cf88' = 'BridgeModificationTimestamp',
'field_619d28814cf89' = 'ListingKey',
];
foreach($fillable as $key = $name ) {
update_field( $key, $brewery-$name, $inserted_brewery );
} //end foreach
} //end foreach
wp_remote_post( admin_url('admin-ajax.php?action=get_breweries_from_api'), [
'blocking' = false,
'sslverify' = false,
'body' = [
'nextLink' = $searchresults //this is what changes the current page above from 1 to 2 (needs to equal the next link)
]
] );
}// end get_brew_from_api function
Topic wp-remote-get wp-insert-post ajax custom-post-types Wordpress
Category Web