仅当数据库更新时,才将所有帖子从数据库导出到JSON

时间:2016-07-20 作者:Achillx

我试图将数据库中所有已发布的帖子导出到JSON文件中,以便在JS中使用它。

代码如下:

<?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\' );
代码在函数内部时工作。php,但它会在每次重新加载时执行,而不是在触发save\\u post挂钩时执行。我把它作为一个插件制作,但while循环中有一个问题,导致整个网站崩溃。

有什么想法吗?

编辑:根据mmm的建议,以下是最终工作代码:

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

1 个回复
最合适的回答,由SO网友:mmm 整理而成

它是file_put_contents 这将保存数据,然后它必须位于挂钩中,而不是插件主体中:

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