Blogging 101

How to bulk export post details from WordPress into CSV without any plugins

Introduction

All bloggers need their post details (like title, permalink, short link, excerpt etc.) preferably in Excel format, for easier accessibility during social media scheduling. However, unless you make it a point to save this information every time you publish a new post, it could get unmanageable pretty fast. The master sheet could also end up being out of sync if halfway through your blogging journey, you decide to change your post details (for SEO or Other reasons).

I have found myself in need of bulk exporting the post details every few months, because:

  • A. I am forgetful/lazy and often don’t remember to update my master excel.
  • B. I keep experimenting with post titles and excerpts every now and then.
  • C. I use Jetpack’s WP.me short links and it is much easier to bulk extract them alongside the rest of the details, instead of doing them manually, one at a time.
But why not use a plugin?

The question that arises next is why not use a plugin for the extraction instead of changing your theme’s code base. Well, you could try a plugin. But the challenge that I faced when investigating a suitable plugin is that none of them gave me the option to export short links without a premium subscription (Short links are essential for Twitter, so I did not want to skip them!).

So, if you are interested in a free, simple PHP code hack to get this done, then read on.

How to bulk export post details from WordPress into CSV

Step1: Create a backup copy of your single.php file

Before making any changes to your codebase, you must create a backup copy of your running single.php code. If your theme uses custom PHP files for displaying posts (instead of single.php), you will need to backup and make changes to them.

Step2: Copy the bulk export code to single.php file

Copy and paste the below code at the bottom of your single.php file. Please ensure that the file does not have any compilation errors due to tag mismatch etc.

CLICK IMAGE TO DOWNLOAD AND COPY CODE
Step3: Customize your code

In the above code snippet, I am extracting the post’s permalink, title, excerpt and short link (This information is sufficient for scheduling my Twitter and Facebook feeds). You could add extra fields/functions including post Id, image details (for Pinterest scheduling) and so on if needed. Please refer to “Additional References” section for more information.

It is better to use the tab (“\t”) instead of the comma (“,”) as column delimiters in the code. This is because the title and excerpt may already contain commas in some of your posts leading to CSV display problems.

Depending on your operating system, you may need to use “\r” or “\n” or both for displaying newline correctly in the final csv. (On Mac, only “\n” did not work for me. Also, I have used double quotes (“) instead of single quotes (‘) because it too was giving me issues, although I am not sure why).

In line 3 of the code snippet, ‘showposts=200’ depicts the total number of published posts on your blog. You can customize it to reflect the correct number of posts on your site. [Please note that the amount of time it will take to generate the CSV file will depend on this number. So, if you have a huge list of published posts on your blog, you should make it a point to run this code in batches or only during non-peak hours.]

Few optional customizations
  • For better control, you can also move this code into your theme’s function.php file and invoke it in single.php from there.
  • After the code has been called, you can download the posts.csv file from http://yoursiteurl.com/wp-content/uploads/posts.csv. However, if you wish to save it somewhere else and FTP it from that location, you can do the same by changing the file location in line 2 of the code snippet.
  • If you do not have any short links enabled on your blog, then by default, wp_get_shortlink() will return something like “http://yoursiteurl.com/?p=1077”. However, if you are using Jetpack’s short links or Bitly (or something similar), it will return the WP.me or bit.ly link (as the case may be). As long as your short links plugin is overriding “get_shortlink” filter of WordPress appropriately,  wp_get_shortlink() should work fine.
Step4: Revert your single.php file to its backup copy

Once you have downloaded the CSV file and it looks good, don’t forget to revert back the single.php code to its original form.

Finally, here’s a sample CSV file for your reference based on a test run of the above code snippet (for ‘showposts=2′):

http://fertilebrains.com/coffee-taste-morning/How did your coffee taste this morning?Overthinking the morning cup of coffee and discovering the essence of life, one sip at a timehttp://wp.me/p5hIdx-1wF
http://fertilebrains.com/redefining-life-one-label-time/Redefining life, one label at a timeThe labels we use to define people don’t fit snugly into neat little boxes, rather they are fluid and more.http://wp.me/p5hIdx-1wb

Additional References

There you have it. Simple PHP code snippet to bulk export post information from your WordPress blog to a CSV file. Please let me know your suggestions/questions etc. in the comments section below. Share and pin it too, if you found the post useful!

PIN THIS

Vote For Me @ The Top Mommy Blogs Directory Vote For Me @ The Top Mommy Blogs Directory
 

Leave A Comment

KEEP BROWSING

These words…

This menagerie of decibels, of derived meaning, of speaking and listening, all of these words – A poem…

What brings you joy?

Four simple words. But enough to spiral my brain into a never-ending cycle of overthinking. What is joy? How is it different from happiness? What trig…