During my research to make my new plugin which will find all posted post title and filter them as typing (so I won’t write a duplicate code snippet), found this very usefull snippet.

You can combine and build a plugin, main feature of this snippet is to build a JSON file after the post got updated (so you can use JS with that JSON…)

Plugin Name: Export to JSON
Author: Achilleas X.
Author URI: http://wordpress.stackexchange.com/users/90258/
Description: Every time you save,update or delete a post, all the published post are getting saved in a JSON file in the uploads directory. Have in mind that by default it only exports "title - excerpt - author" , but you can add whatever else you want.

function export_posts_in_json (){

    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,

    $query = new WP_Query( $args );
    $posts = array();

    while( $query->have_posts() ) : $query->the_post();

    $posts[] = array(
        'title' => get_the_title(),
        'excerpt' => get_the_excerpt(),
        'author' => get_the_author()



    $data = json_encode($posts);
    $upload_dir = wp_get_upload_dir();
    $file_name = date('Y-m-d') . '.json';
    $save_path = $upload_dir['basedir'] . '/' . $file_name;

    $f = fopen( $save_path , "w" ); //if json file doesn't gets saved, comment this and uncomment the one below
    //$f = @fopen( $save_path , "w" ) or die(print_r(error_get_last(),true)); //if json file doesn't gets saved, uncomment this to check for errors
    fwrite($f , $data);


add_action( 'save_post', 'export_posts_in_json' );


0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x