Customize JSON output

时间:2017-12-12 作者:Sendul

我正在使用代码从WordPress数据库生成JSON。代码来自Export all post from database to JSON only when the database gets updated

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

    // I add additional data to export
    \'category\' => get_the_category()
);

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\' );
JSON输出为

[
  {
    "title": "My Post 1",
    "excerpt": "My Post 1 excerpt",
    "author": "Admin",
    "category": [
      {
        "term_id": 53,
        "name": "My Category 1",
        "slug": "my-category-1",
        "term_group": 0,
        "term_taxonomy_id": 53,
        "taxonomy": "category",
        "description": "",
        "parent": 7,
        "count": 4,
        "filter": "raw",
        "meta": [

        ],
        "cat_ID": 53,
        "category_count": 4,
        "category_description": "",
        "cat_name": "My Category 1",
        "category_nicename": "my-category-1",
        "category_parent": 7
      }
    ]
  },
  {
    "title": "My Post 2",
    "excerpt": "My Post 2 excerpt",
    "author": "Admin",
    "category": [
      {
        "term_id": 28,
        "name": "My Category 2",
        "slug": "my-category-2",
        "term_group": 0,
        "term_taxonomy_id": 28,
        "taxonomy": "category",
        "description": "",
        "parent": 7,
        "count": 5,
        "filter": "raw",
        "meta": [

        ],
        "cat_ID": 28,
        "category_count": 5,
        "category_description": "",
        "cat_name": "My Category 2",
        "category_nicename": "my-category-2",
        "category_parent": 7
      }
    ]
  }
]
我的目标是生成如下所示的JSON输出

{
    "Categories": 
        [
            {
                "name": "My Category 1"
            },
            {
                "name": "My Category 2"
            },
        ],

    "Posts":
        [
            {
                "title": "My Post 1",
                "excerpt": "My Post 1 excerpt",
                "author": "Admin",
                "category": "My Category 1"
            },
            {
                "title": "My Post 2",
                "excerpt": "My Post 2 excerpt",
                "author": "Admin",
                "category": "My Category 2"
            }
        ]
}
更新:使用motivast的代码,通过删除$categories = array_unique($categories);

JSON输出如下所示

{
    "Categories": 
        [
            {
                "name": "My Category 1"
            },
            {
                "name": "My Category 2"
            },
            {
                "name": "My Category 2"
            },
        ],

    "Posts":
        [
            {
                "title": "My Post 1",
                "excerpt": "My Post 1 excerpt",
                "author": "Admin",
                "category": "My Category 1"
            },
            {
                "title": "My Post 2",
                "excerpt": "My Post 2 excerpt",
                "author": "Admin",
                "category": "My Category 2"
            },
            {
                "title": "My Post 3",
                "excerpt": "My Post 3 excerpt",
                "author": "Admin",
                "category": "My Category 2"
            }
        ]
}
更接近我的目标,删除类别中的重复内容。

更新2:根据TomJ Nowell的建议,现在我可以使用REST API post端点获得我想要的东西。当然,Motivast的代码在重新安排数据结构方面帮了我很大的忙。

谢谢你,汤姆。

非常感谢莫蒂瓦斯特。

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

此代码将执行以下操作:

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

    $query = new WP_Query( $args );

    $posts = array();
    $categories = array();
    $json = array();

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

        $post_categories = get_the_category();

        $post_category = current($post_categories);

        $categories[] = array(
            \'name\' => $post_category->name
        );

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

            // I add additional data to export
            \'category\' => $post_category->name
        );

    endwhile;

    $categories = array_unique($categories);

    $json[\'Categories\'] = $categories;
    $json[\'Posts\'] = $posts;

    $data = json_encode($json);

结束