1

I am trying to import post meta through the REST API. I have the meta value set on the parent site, but when I import the post to the child site there are additional characters added to the value.

Here is the JSON representation of the post meta from the parent site:

 "post-meta-fields": {
            "_edit_lock": [
                "1498250457:1"
            ],
            "_edit_last": [
                "1"
            ],
            "color": [
                "#4298f4"
            ],
            "_thumbnail_id": [
                "6"
            ]
        },

And here is the meta data after import to the child site:

"post-meta-fields": {
            "city_name": [
                "city_Columbus"
            ],
            "import": [
                "import"
            ],
            "color": [
                "a:1:{i:0;s:7:\"#4298f4\";}"
            ]
        }

And finally, here is the cod I use to import:

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

    }

}

I have not been able to find any info on this issue online, so any help is appreciated!

1
  • a:1:{i:0;s:7:\"#4298f4\";} is the serialized array a:1:{i:0;s:7:"#4298f4";} with some escaping added to it. I don't have time for a full answer, but wanted to point that hint out. Jun 24 '17 at 0:16
0

The answer was that I was not referencing the 0 index of the json post-meta-fields array. It was trying to return an array instead of just the string.

Instead of

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

I needed

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.