Adding Export Filters for Custom Post Types
I recently needed to export a single post for a client. I thought it through and decided that I would create a unique author, assign the post to the author, then use the Authors filter in the WordPress Export Tool to export the only post by the author. It seemed like a good plan until I realized that export filters are not available to custom post types and, as I’m sure you can now easily figure out, the post in question was in fact a custom post.
Now, I’m sure there’re some plugins out there that could’ve solved this problem for me. However, there’s a kink in my DNA or something that makes me want to figure it out myself. I suppose that’s a big part of why I do what I do for both work and leisure.
I did some searching, and it turns out that this is one those increasingly rare subjects in the WordPress universe that has very little written about it. I was finally able to find a reference to a blog post on the subject, but, when I followed it, the site was no longer up. So, I hit the Wayback Machine and retrieved the post.
Here it is as code. All commentary has been converted to comments.
The example adds start and end date dropdowns so that you can filter a custom post type named ‘reviews’ by date. To use this with a custom post type other than reviews, you would replace every instance of reviews with the name of another custom post type.
In the use case mentioned above, I could set the published date of the single post that I needed to export to a day on which no other posts of that type were published.
A good deal of the code above was appropriated from the WordPress core at wp-admin/includes/export.php. When I get a chance, I’ll take a crack at making all of the filters available to posts and pages available to a custom post type. From there, I could see some value in making these filters available to all custom post types. I’ll follow up in a subsequent post.