4

We have received a URL which is a JSON file apparently:

http://www.domain.com/tools/export-json/?destination=hawaii

I am completely clueless when it comes to JSON and Wordpress.

Anybody know where to start?

We will need to create individual posts from this JSON file I presume..

1
  • There are some PHP native function like json_encode() and json_decode()
    – JMau
    Jun 7 '13 at 18:31
11
  1. json_decode the JSON into an array.

    $slices = json_decode(file_get_contents('yourJSONFile.json'),true);
    
  2. Loop into the data

    if ($slices) { 
       foreach ($slices as $slice) {
          $title = $slice[1];
          // insert more logic here
      }
    } 
    
  3. Create a post programmatically by using wp_insert_post.

    // Create post object
    $my_post = array(
         'post_title'    =>  $title,
         'post_content'  => 'This is my content',
         'post_status'   => 'publish',
         'post_author'   => 1,
         'post_category' => array(8,39)
    );
    
    // Insert the post into the database and return the new post ID
    $post_id = wp_insert_post( $my_post, true );
    if ( is_wp_error( $post_id ) ) {
         // error handling....
    }
    

More details in this tutorial: http://tommcfarlin.com/programmatically-create-a-post-in-wordpress/

3
  • Please try to include at least summary of information from URL into answer. Links tend to rot, making answers crippled.
    – Rarst
    Jun 7 '13 at 21:17
  • ok thanks for this - it is useful info - if you are aware of any relevant wordpress plugins that would be great too.. Jun 8 '13 at 12:33
  • This solution works but is giving me load time error. Is there a faster way for 1000s of posts?
    – user2238
    Mar 1 '17 at 21:56

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.