How To Export Posts To CSV In WordPress

WordPress is a blogging platform. A blog is nothing but a collection of posts. In some scenarios, you may want to share your list of posts with someone via the CSV file. It will be helpful to take a quick look at all posts without login to the dashboard. Using such CSV you can easily find the post title and their permalink. In this article, I show you how to export posts to CSV in WordPress.

I am going to add one custom button on the backend post listing page. On click of this button, the CSV file would download automatically which contains information about posts.

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

Export Posts To CSV

Add Custom Button on the Post Listing

To generate the CSV, we first need to give a button on the backend post listing. Additionally, we will write code that generates a CSV on the click of 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="export_all_posts" class="button button-primary" value="<?php _e('Export All Posts'); ?>" />

add_action( 'manage_posts_extra_tablenav', 'admin_post_list_add_export_button', 20, 1 );

The above code will add the ‘Export All 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 ‘All Posts’ page.

Export Post Button

Actual Code for Exporting Posts to CSV

At this stage, you are ready with your custom button which should generate the CSV. Now, let’s add a code that generates the CSV on the click of a button and send it to the browser for downloading it.

Add the below code in the functions.php file.

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-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 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_title(), get_the_permalink(), implode(",", $cats), implode(",", $tags)));

add_action( 'init', 'func_export_all_posts' );

That’s it! Go ahead and give it a try. You should get the CSV file along with your post’s information.

I hope you understand how to export posts to CSV in WordPress. Please share your thoughts and suggestions in the comment section below.

Related Articles

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

27 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.

  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 *