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..
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
-
json_decode the JSON into an array.
$slices = json_decode(file_get_contents('yourJSONFile.json'),true); -
Loop into the data
if ($slices) { foreach ($slices as $slice) { $title = $slice[1]; // insert more logic here } } -
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/
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0