Export all post from database to JSON only when the database gets updated

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

Leave a Comment