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.
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'); ?>" />
<?php
}
}
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.
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) {
setup_postdata($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)));
}
exit();
}
}
}
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
- How to Add Code After the Body Tag in WordPress
- Adding Custom Image Button with Media Uploader in WordPress
- Display Posts from Specific Category On A WordPress Page
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
Thank you man, for sharing this code!!!
Helped me a lot!!!!!
You’re welcome, Shlomi.
It was helpful.
Thank you!
You’re welcome, Harry.
Hi, how can i export selected post on admin column?
Thanks
I’ll soon try to update the article with this option.
I updated the article. It now allows exporting selected posts.
SO helpful, thank you!
This was great! I’d love to add the post ID number to the list too… I don’t know enough code to do that…
Add ‘Post ID’ in the heading and then use the method
get_the_ID()
to fetch post ID.amazing brother, you saved my time.. Thank you so much..
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?
I updated the article. It now includes categories and tags also.
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
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.
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.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
You can use get_field(‘keyname’) or get_field(‘keyname’, get_the_ID()) in the loop.
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
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”
Thanks for letting me know. I have updated the article.
Would anyone know what we can add to this to pull the actual post and its comments?
Thank you!
How to use link as download button. Trying to add the download button next to the add new button.
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!!!
Nice post.
Is there a way to add the filter options to the export button.
https://generatewp.com/filtering-posts-by-taxonomies-in-the-dashboard/
Yes, it can be possible. We can get the value of filters from GET request and modify our arguments.
Hi Sajid
Thank you very much. I made this but it does not seem to work:
https://pastebin.com/rfyTFXaX
Can you help me ?
For whom it may concern…
https://wordpress.stackexchange.com/questions/321474/query-a-custom-taxonomy-in-a-function-to-create-an-csv-file
how to export page data to CSV please help me i pretty new on WordPress
Thanks in advance.
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’
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!
You need to modify the query in the method ‘func_export_all_posts’.
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.
Thank you.How can I get category name and tag name when export to csv file?
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) {
setup_postdata($post);
fputcsv( $file, array( get_the_permalink() , get_the_title() ) );
}
exit();
}
}
}
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!
It was helpful.
Thank you!