How to Export Posts to CSV in WordPress

WordPress is a popular blogging platform. A blog is nothing but a collection of posts. WordPress provides the option at the backend to add and view your posts. By login to the dashboard, you can easily access your posts. However, in some scenarios, you may want to share your list of posts via the CSV file. It will be helpful to take a quick look at all posts without accessing the dashboard. Using such CSV you can easily find the details like post id, post title, post permalink, etc. In this article, I show you how to export posts to CSV in WordPress.

I am going to add a custom button on the post-listing page. With a click of this button, the CSV file would download automatically. This CSV file will have information about your posts.

As an example, I am exporting a post ID, post title, post URL, categories, and tags to the CSV file. The user can adjust these fields in the CSV. The final output will be something like the below screenshot.

Export Posts To CSV

Add Custom Button on the Post Listing

To create the CSV, you need a button on the post listing to trigger the export action. We will write code that generates a CSV on clicking this button.

Open your functions.php file and place the below code in it.

function admin_post_list_add_export_button( $which ) {
    global $typenow;
    if ( 'post' === $typenow && 'top' === $which ) {
        <input type="submit" name="aweb_export_posts" class="button button-primary" value="<?php _e('Export Posts'); ?>" />
add_action( 'manage_posts_extra_tablenav', 'admin_post_list_add_export_button', 20, 1 );

The above code will add the ‘Export Posts’ button on the posts listing as shown in the screenshot. Here, I have used the hook manage_posts_extra_tablenav to place the custom button on the listing page.

Export Post Button

Code for Exporting Posts to CSV

You are ready with your custom button which should create the CSV. Now, let’s add a code that generates the CSV with the click of a button. I’ll send the generated CSV to the browser which will be downloaded automatically.

Add the below code in the functions.php file.

function aweb_export_posts() {
    if(isset($_GET['aweb_export_posts'])) {
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',

        if ( isset($_GET['post']) ) {
            $args['post__in'] = $_GET['post'];
        } else {
            $args['posts_per_page'] = -1;
        global $post;
        $arr_post = get_posts($args);
        if ($arr_post) {
            header('Content-type: text/csv');
            header('Content-Disposition: attachment; filename="wp-posts.csv"');
            header('Pragma: no-cache');
            header('Expires: 0');
            $file = fopen('php://output', 'w');
            fputcsv($file, array('Post ID', 'Post Title', 'URL', 'Categories', 'Tags'));
            foreach ($arr_post as $post) {
                $categories = get_the_category();
                $cats = array();
                if (!empty($categories)) {
                    foreach ( $categories as $category ) {
                        $cats[] = $category->name;
                $post_tags = get_the_tags();
                $tags = array();
                if (!empty($post_tags)) {
                    foreach ($post_tags as $tag) {
                        $tags[] = $tag->name;
                fputcsv($file, array(get_the_ID(), get_the_title(), get_the_permalink(), implode(",", $cats), implode(",", $tags)));

add_action( 'admin_init', 'aweb_export_posts' );

That’s it! Go ahead and give it a try. You should get the CSV file along with your post’s information. The user can also export a few posts by selecting their respective checkboxes.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

38 thoughts on “How to Export Posts to CSV in WordPress

  1. Are you able to provide some detail into how to export all post meta? Title & URL is a great but is it possible to include categories & tags?

  2. Hi , thanks for sharing , can tellme how it work with filter post ?

    example i filter in January 2020 this function only export post on date january 2020 thanks before

  3. Hi,

    Thanks for sharing a nice code without using any plugin.

    Just wanted to ask how can i export custom fileds in the csv, as i am using ACF Pro plugin for custom field.

    1. In that case, you need to write a code to get custom field value and then append these values to the CSV array.
      ACF provides a get_field() method to get the values of fields.

      1. Hello

        Good Evening can you describe about the how to get the acf field value

        how to map within array
        Please tell me , i am waiting your reply

          1. Thanku So much

            For your reply

            I have custom taxonmoy

            This Is my code

            $locations = get_terms([‘taxonomy’=>’Location’]);
            $locat = array();
            if (!empty($locations)) {
            foreach ( $locations as $location ) {
            $locat[] = $location->name;

            Display on csv file All category

            Did you check this is my code right ya wrong
            I am wating you reply
            thank you

  4. Thank you for this tutorial.
    To avoid using javascript to move the button you can use the hook “manage_posts_extra_tablenav” instead of “restrict_manage_posts”

  5. Hi,
    I have been using code quite successfully for exporting posts and pages data. I had two queries if you could possible answer:
    1) I have a custom post type. How to export data for CPTs?
    2) I am using a plugin to create coupons and deals – WP Coupons and Deals. They are published as coupons and not custom post types. Is there a way to export coupons list successfully into CSV?
    Thanks in Advance!!!

    1. You need to do 2 changes then.
      1) change ‘edit.php’ == $screen->parent_file to ‘edit.php?post_type=page’ == $screen->parent_file
      It will add export button on page list screen.
      2) change ‘post_type’ => ‘post’ to ‘post_type’ => ‘page’

  6. Quick question: is there a way to export just the filtered query and not all of the posts? Thank you very much and really great post!

  7. This is a neat little piece of code to solve a common problem while backtracking the lists of posts published by us or to send a report to another person.

  8. Added UTF 8 Character Problem:

    /* Export Core Function */
    add_action( ‘init’, ‘func_export_all_posts’ );
    function func_export_all_posts() {
    if(isset($_GET[‘export_all_posts’])) {
    $arg = array(
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1,

    global $post;
    $arr_post = get_posts($arg);
    if ($arr_post) {

    header(‘Content-Encoding: UTF-8’);
    header(‘Content-Type: text/csv; charset=utf-8’ );
    header(sprintf( ‘Content-Disposition: attachment; filename=my-csv-%s.csv’, date( ‘dmY-His’ ) ) );
    header(‘Content-Transfer-Encoding: binary’);
    header(‘Expires: 0’);
    header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0’);
    header(‘Pragma: public’);

    $file = fopen(‘php://output’, ‘w’);

    fputs( $file, “\xEF\xBB\xBF” ); // UTF-8 BOM !!!!!

    fputcsv($file, array(‘URL’, ‘Гарчиг’));
    foreach ($arr_post as $post) {
    fputcsv( $file, array( get_the_permalink() , get_the_title() ) );


    1. Hey thanks for the tutorial and the fix 🙂
      I have changed the code for this one and I have issues with the accents (á é ect)
      Not the biggest problem but just to let you know 🙂

      Thanks again!

Leave a Reply

Your email address will not be published. Required fields are marked *