hooks – How to export category name and category link in wordpress json

Question

all queries work except category name and category link, if I use “get_the_category()” it shows several things I don’t need, like “category_count” , “category_description” etc.

and i need to know how to separate jsons by post category

this is my code

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

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

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

    $posts[] = array(
        'id' => get_the_ID(),
        'date' => get_the_date(),
        'title' => get_the_title(),
        'link' => get_the_permalink(),
        'image' => get_the_post_thumbnail_url(),
        'category' => get_the_category ( 'cat_name' ),
        'categoryLink' => get_category_link ( $category_id )

    );

    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'] . "https://wordpress.stackexchange.com/" . $file_name;

    $f = fopen( $save_path , "w" ); 
    fwrite($f , $data);
    fclose($f);

}

add_action( 'save_post', 'export_posts_in_json' );

0
Rafael Medeiros 5 months 2021-07-04T13:07:33-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse