wp_insert_post run inside foreach loop
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.
php wordpress
add a comment |
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.
php wordpress
dump your $post_data first. exit the loop on first loop to see if the post is inserted twice.
– TurtleTread
Jun 28 '17 at 19:30
The problem seems to be coming from the last foreach loop that runs, if I pass more then one URL to the foreach loop it creates the duplicates of each post.
– Top-Bot
Jun 28 '17 at 19:56
add a comment |
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.
php wordpress
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.
php wordpress
php wordpress
edited Jun 28 '17 at 19:34
Top-Bot
asked Jun 28 '17 at 17:06
Top-BotTop-Bot
160112
160112
dump your $post_data first. exit the loop on first loop to see if the post is inserted twice.
– TurtleTread
Jun 28 '17 at 19:30
The problem seems to be coming from the last foreach loop that runs, if I pass more then one URL to the foreach loop it creates the duplicates of each post.
– Top-Bot
Jun 28 '17 at 19:56
add a comment |
dump your $post_data first. exit the loop on first loop to see if the post is inserted twice.
– TurtleTread
Jun 28 '17 at 19:30
The problem seems to be coming from the last foreach loop that runs, if I pass more then one URL to the foreach loop it creates the duplicates of each post.
– Top-Bot
Jun 28 '17 at 19:56
dump your $post_data first. exit the loop on first loop to see if the post is inserted twice.
– TurtleTread
Jun 28 '17 at 19:30
dump your $post_data first. exit the loop on first loop to see if the post is inserted twice.
– TurtleTread
Jun 28 '17 at 19:30
The problem seems to be coming from the last foreach loop that runs, if I pass more then one URL to the foreach loop it creates the duplicates of each post.
– Top-Bot
Jun 28 '17 at 19:56
The problem seems to be coming from the last foreach loop that runs, if I pass more then one URL to the foreach loop it creates the duplicates of each post.
– Top-Bot
Jun 28 '17 at 19:56
add a comment |
1 Answer
1
active
oldest
votes
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 = "<?phpnn$text = $var_str;nn?>";
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]);
}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44808702%2fwp-insert-post-run-inside-foreach-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 = "<?phpnn$text = $var_str;nn?>";
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]);
}
}
}
add a comment |
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 = "<?phpnn$text = $var_str;nn?>";
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]);
}
}
}
add a comment |
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 = "<?phpnn$text = $var_str;nn?>";
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]);
}
}
}
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 = "<?phpnn$text = $var_str;nn?>";
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]);
}
}
}
answered Jun 29 '17 at 14:38
Top-BotTop-Bot
160112
160112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44808702%2fwp-insert-post-run-inside-foreach-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
dump your $post_data first. exit the loop on first loop to see if the post is inserted twice.
– TurtleTread
Jun 28 '17 at 19:30
The problem seems to be coming from the last foreach loop that runs, if I pass more then one URL to the foreach loop it creates the duplicates of each post.
– Top-Bot
Jun 28 '17 at 19:56