wp_insert_post在foreach循环中运行

时间:2017-06-28 17:06:15

标签: php wordpress

我在这里遇到了一些挑战,我正在从多站点网络上的站点的wp rest api导入后期数据。将url添加为选项,然后使用带有自定义函数的foreach循环遍历每个选项。如下所示。

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();
}

这是create_post_from_url()函数

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);
      }
    }

当该功能运行时,它会创建每个帖子中的2个,我无法追踪此问题,所以任何帮助都表示赞赏!

编辑:如果我将$ post_url的值设置为单个URL(e.x http://example.com/wp-json/wp/v2/posts),那么它运行没有问题。当我尝试在import_posts()函数的foreach循环中运行create_import_post_from_url()时,似乎会出现问题。

1 个答案:

答案 0 :(得分:0)

答案是我检查现有帖子的if语句的最后一个括号没有放在wp_insert_posts函数下面。

以下是其他人偶然发现的纠正功能

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\n\n\$text = $var_str;\n\n?>";
    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]);
    }
}
}
相关问题