0

I'm trying to export all the published post of my database to a JSON file, in order to use it in JS.

Here is the code:

<?php
/*
Plugin Name: Export to JSON
Description: Creates the JSON file that stores all the necessary post data that are
used in the timeline js
*/

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();

    // Add a car entry
    $posts[] = array(
        'title' => get_the_title(),
        'excerpt' => get_the_excerpt(),
        'author' => get_the_author()
    );

    endwhile;

    wp_reset_query();

    return json_encode($posts);
}

$folder = 'wp-content/themes/bootstrap/library/';
$file_name = date('Y-m-d') . '.json';
file_put_contents($folder.$file_name, export_posts_in_json());

add_action( 'save_post', 'export_posts_in_json' );

The code works when it is inside functions.php but it gets executed in every reload and not when the save_post hook gets triggered. I've made it as a plugin but there is a problem in the while loop and crashes the whole site.

Any ideas?

Edit: After mmm's suggestion, here is the final working code:

<?php
/*
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()
    );

    endwhile;

    wp_reset_query();

    $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);
    fclose($f);

}

add_action( 'save_post', 'export_posts_in_json' );
0

it's file_put_contents which saves the data then it have to be in the hook and not in the body of the plugin :

function export_posts_in_json () {

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

    $query = new WP_Query( $args );

    ...

    $data = json_encode($posts);

    $folder = 'wp-content/themes/bootstrap/library/';
    $file_name = date('Y-m-d') . '.json';
    file_put_contents($folder.$file_name, $data);
}


add_action( 'save_post', 'export_posts_in_json' );
  • I suggest to use save_post_post hook instead of save_post as OP is only looking for posts. Also check for whether we are on the new page or published page either using $pagenow global or using save_post_post third parameter $update – bravokeyl Jul 20 '16 at 12:35
  • Great and usefull plugin, thanks, it helped me a lot to make a custom events calendar with ACF and Monthly.js. Just one thing I had to fix was to add a 'return' statement after fclose($f) because I was getting a blank page after action was called. So my final code was: /* ... */ fwrite($f , $data); fclose($f); return; – jasubal Jul 10 '18 at 16:16

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.