I have a bit of a challenge here, I am importing post data from the wp rest api of sites on a multisite network. The urls are added as options and then I run through each option with a foreach loop with a custom function as seen below.
function import_posts() {
global $wpdb;
$query_name = $wpdb->prefix . 'options';
$options = $wpdb->get_results("SELECT option_name, option_value FROM $query_name WHERE option_name LIKE 'city_%'");
// array of option names
foreach ($options as $key => $row) {
$url = $row->option_value;
$city_name = $row->option_name;
create_import_post_from_url($url,$city_name);
}
echo"The Import Is Finished";
wp_die();
}
And here is the create_post_from_url() function
function create_import_post_from_url($url, $city_name ) {
$post_url = file_get_contents($url);
$post_data = json_decode($post_url, true);
$i=1;
foreach($post_data as $article_array ) {
$post_color = $article_array['post-meta-fields']['color']['0'];
$post_image = $article_array['post-meta-fields']['feat_url']['0'];
$title = $article_array['title']['rendered'];
$post_title_check = get_page_by_title( $title, OBJECT, 'post' );
if ($post_title_check == NULL){
$post_args = array(
'post_author' => $article_array['author'],
'post_content' => $article_array['content']['rendered'],
'post_content_filtered' => '',
'post_title' => $article_array['title']['rendered'],
'post_excerpt' => $article_array['excerpt']['rendered'],
'post_status' => $article_array['status'],
'post_type' => $article_array['type'],
'comment_status' => $article_array['comment_status'],
'ping_status' => $article_array['ping_status'],
'post_password' =>$article_array['post_password'],
'to_ping' => $article_array['to_ping'],
'pinged' => $article_array['pinged'],
'post_parent' => $article_array['post_parent'],
'menu_order' => $article_array['menu_order'],
'guid' => $article_array['guid']['rendered'],
'import_id' => 0,
'context' => '',
'meta_input' => array(
'city_name' => $city_name,
'import' => 'import',
'color' => $post_color,
'featured_image' => $post_image,
)
);
}
wp_insert_post($post_args, $wp_error);
}
}
When the function is run it creates 2 of each post, I am having trouble tracking down this issue so any help is appreciated!
EDIT: If I set the value of $post_url to a single url (e.x http://example.com/wp-json/wp/v2/posts) then it runs without a problem. The problem seems to happen when I try to run the create_import_post_from_url() in the foreach loop of the import_posts() function.
The answer is that my final bracket for the if statement that checked for existing posts was not placed below the wp_insert_posts function.
Here is the corrected function in case anyone else stumbles across this
function create_import_post_from_url($url, $city_name ) {
$post_url = file_get_contents($url);
$post_data = json_decode($post_url, true);
$i = 1;
foreach($post_data as $article_array ) {
$post_color = $article_array['post-meta-fields']['color']['0'];
$post_image = $article_array['post-meta-fields']['feat_url']['0'];
$title = $article_array['title']['rendered'];
$post_title_check = get_page_by_title( $article_array['title']['rendered'], 'OBJECT', 'post' );
if ($post_title_check == NULL){
$var_str = var_export($article_array['title']['rendered'], true);
$var = "<?php
\$text = $var_str;
?>";
file_put_contents('url'. $i++ .'.php', $var);
$post_args = array(
'post_author' => $article_array['author'],
'post_content' => $article_array['content']['rendered'],
'post_content_filtered' => '',
'post_title' => $article_array['title']['rendered'],
'post_excerpt' => $article_array['excerpt']['rendered'],
'post_status' => $article_array['status'],
'post_type' => $article_array['type'],
'comment_status' => $article_array['comment_status'],
'ping_status' => $article_array['ping_status'],
'import_id' => 0,
'context' => '',
'meta_input' => array(
'city_name' => $city_name,
'import' => 'import',
'color' => $post_color,
'featured_image' => $post_image,
)
);
wp_insert_post($post_args, $wp_error);
unset($post_data[$article_array]);
}
}
}