Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

John Nixon
John Nixon
17,690 Points

Anyone had any experience using a JSON file to create and update a custom post types posts?

I'm working on a project where the client has requested we bring in a feed from a third party service. They've provided us with a JSON to get the content. I've followed along with the tutorial on building a plugin and it is very similar to what I need. The biggest difference is I need the entries to save and update as posts. I have a lot of it done, but its not quite where I would like it to be. I can't get posts to update without them duplicating and I've not even gotten into modifying the AJAX yet. If you have done this or know of any resources that you can point me to it would be a big help.

3 Answers

John, for this to work, you'll need to make sure each new "post" from the JSON feed has some sort of unique identifier. When doing your loop to create the new posts, you can check if a post already exists for that unique ID, and if it does, there are functions to update that post information and meta on the fly.

As a side note, you'll definitely want to cache that JSON feed data or run it on a CRON job so that way your page loads aren't insane. This is especially true if the feed has hundreds of posts in it.

John Nixon
John Nixon
17,690 Points

Thanks for the input, I've already gotten the unique identifier set up and as far as the page load time its not a big deal because as I said they are saving in the database as actual posts. Where I'm stuck at now is getting the posts to update, and getting the ajax going but here is a snippet of how I'm currently adding the posts

foreach($winston_salem_event_feed->{'results'} as $event){
                                //variables created from json feed array
                $title            =  $winston_salem_event_feed->{'results'}[$titleCount++]->{'name'};
                $slug             =  "ew" . $winston_salem_event_feed->{'results'}[$slugCount++]->{'id'};
                $postDate         =  $winston_salem_event_feed->{'results'}[$postDateCount++]->{'posted'};
                $content          =  $winston_salem_event_feed->{'results'}[$contentCount++]->{'description'};
                $featuredImageURL =  $winston_salem_event_feed->{'results'}[$featuredImageURLCount++]->{'thumbnail'};

                //check to see if a post exists by the slug
                if (!the_slug_exists("$slug")) {

                    //create a new post

                    $event_post = array(
                        'post_title'    =>  $title,
                        'post_name'     =>  $slug,
                        'post_date'     =>  $postDate, 
                        'post_content'  =>  $content,
                        'post_status'   =>  'publish',
                        'post_author'   =>  1,
                        'post_type'     =>  'events'
                    );

                    $post_id = wp_insert_post( $event_post, $wp_error );

                                       //adds values to custom post fields
                    update_post_meta( $post_id,  'wpcf-featured-image-url', "$featuredImageURL" );

                } else {

                    /* update existing posts, here is where I'm stuck 
                      * I've got to figure out how to get the actual post id
                                          * of anything that is currently a post in order to update it
                                        */

                }

            };
'''
Ryan Carter
Ryan Carter
726 Points

Hey John,

Did you manage to get this working?

Christopher Nowlan
Christopher Nowlan
2,582 Points

Hi John, Could you post the working example?