write a custom simple script that retrieves the json data and creates wp posts using the wp REST API and replace the $json_url variable with the URL of your JSON endpoint
we added a post_exists() function to check if a post with the same title already exists in the wp database
// URL of the JSON endpoint
$json_url = 'https://api.webscraper.io/api/v1/scraping-job/4851593/json?api_token=aLXQWr2IbQCefgiLc1PfIjfx7GqvsBd3APVbU1pHchszzzFIFa6HFKsNmpft';
// Retrieve JSON data
$response = file_get_contents($json_url);
// Check if data was successfully retrieved
if ($response === false) {
die('Failed to fetch JSON data.');
}
// Convert JSON data to associative array
$data = json_decode($response, true);
// Check if JSON decoding was successful
if ($data === null) {
die('Failed to decode JSON data.');
}
// Function to check if post already exists
function post_exists($title) {
global $wpdb;
$query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'post'", $title);
$post_id = $wpdb->get_var($query);
return $post_id;
}
// Loop through each item in the JSON data
foreach ($data as $item) {
// Check if post already exists
if (post_exists($item['title'])) {
echo 'Post with title "' . $item['title'] . '" already exists. Skipping...<br>';
continue;
}
// Create new post
$post_data = array(
'post_title' => $item['title'], // Assuming 'title' field exists in your JSON data
'post_content' => $item['content'], // Assuming 'content' field exists in your JSON data
'post_status' => 'publish', // Publish the post immediately
// You can include other fields like 'post_excerpt', 'post_category', 'tags_input', etc. as needed
);
// Insert the post into WordPress
$post_id = wp_insert_post($post_data);
// Check if post was inserted successfully
if (is_wp_error($post_id)) {
echo 'Failed to insert post: ' . $post_id->get_error_message();
} else {
echo 'Post inserted successfully with ID: ' . $post_id . '<br>';
}
}