添加到元数据值的未知字符

时间:2017-06-23 作者:Top-Bot

我正在尝试通过RESTAPI导入post meta。我在父站点上设置了元值,但当我将帖子导入子站点时,会在值中添加其他字符。

下面是父站点的post meta的JSON表示:

 "post-meta-fields": {
            "_edit_lock": [
                "1498250457:1"
            ],
            "_edit_last": [
                "1"
            ],
            "color": [
                "#4298f4"
            ],
            "_thumbnail_id": [
                "6"
            ]
        },
下面是导入到子站点后的元数据:

"post-meta-fields": {
            "city_name": [
                "city_Columbus"
            ],
            "import": [
                "import"
            ],
            "color": [
                "a:1:{i:0;s:7:\\"#4298f4\\";}"
            ]
        }
最后,这里是我用来导入的cod:

function create_import_post_from_url($url, $post_args, $city_name ) {
$post_url = file_get_contents($url);
  $post_data = json_decode($post_url, true); 

  foreach($post_data as $article_array ) {
      $title = $article_array[\'title\'][\'rendered\'];
      if (!post_exists($title) ){
    if( empty($post_args)) {
        $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\' => $article_array[\'post-meta-fields\'][\'color\'],
)
        );

    }
     wp_insert_post($post_args, $wp_error);
      }

    }

}
我一直无法在网上找到有关此问题的任何信息,因此非常感谢您的帮助!

1 个回复
SO网友:Top-Bot

答案是我没有引用json post元字段数组的0索引。它试图返回一个数组,而不仅仅是字符串。

而不是

 \'color\' => $article_array[\'post-meta-fields\'][\'color\']
我需要

\'color\' => $article_array[\'post-meta-fields\'][\'color\'][0]

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在