Filter and Hooks

PHP code to Import the POST from one website to another

Are you trying to import any post type contents from one website to another website ?

1. Create a folder in the root directory of the site i.e PARENT_SITE. I have created services folder in the root directory and created the php file named news.php

2. Include wp-load.php the files so that we could use all the WordPress available functions

3. Write a code that encode all the posts/contents related to ‘post_type’=>’post’. We can replace ‘post_type’ => ‘post’ by any other custom post types

We can check the encoded post contents in the Url : http://PARENT_SITE/services/news.php

4. Create a folder in the root directory of the site, I call it AS CHILD_SITE where we want to import the all the contents of posts from the PARENT_SITE. I have created app folder in the root directory of CHILD_SITE and created the php file named import-news.php inside the folder:

5.   I have created custom post type named  ‘imported-news’ and two meta fields , using acf to save the thumbnail  and permalink of imported posts

p.s. I   like to display the thumbnail of news directly from the source instead of saving it in my site and I  might need the source of the  news in the future

6. I have created main_site_url  field using ACF  in Options Page so that user can input the source url from where they can get the json encoded posts to be imported

7.  I have created Importer class that handles the import part.  Provided the post_type and source link of import in the Object of Importer Class and in return calling import method, it works like a charm . To parse the json encoded contents  I have used Guzzle

8. Before importing the posts, we shall delete existing posts and meta related to imported post_type

9. After deleting the posts and meta’s , we shall insert the imported post and categories. If the imported category doesn’t exist, we shall create it and assign the post to the created category.

10. Last but not the list,  run the import file in the CHILD_SITE to import the news  : http://CHILD_SITE/app/import-news.php

P.S. The process is long and I haven’t displayed any screen-shots so that you could try it on your own; provided the full code 😛

Hits: 25

Standard