Yet another programmer blogging about code

Remove the WordPress post date filter on custom post type admin pages

WordPress custom post types can be very useful for storing all sorts of different types of data in WordPress — and I should really write a post about that some time. But the date a post was published, i.e. its post_date, isn’t important for many custom post types. So why have a drop-down list of dates to filter your custom posts types by if you don’t need it?

For a recent job, I created a custom post type for Stores, with a couple of useful custom taxonomies called Brand and Product Type that I can filter by. I have no need to filter by the post date, it is of no use to my client. Some might say it’s useful for finding the most recently added store — but the client actually searches by name, brands sold, and products sold, not by when they added the store.

So here’s what I get normally:

Custom post type Stores with unwanted dates filter
Custom post type Stores with unwanted dates filter

And here’s what I want:

Custom post type Stores without a date filter
Custom post type Stores without a date filter

Removing that little drop-down box was very easy. All I needed to do was replace the list of post dates with an empty list, and WordPress dropped the drop-down. And all it took was this little filter:

add_filter('months_dropdown_results', '__return_empty_array');

OK, yes, I needed to ensure that was only called for my custom post type, but I already do that by separating its admin functionality into its own class which I load based on the value of $typenow.

Note: WordPress has a nice little collection of handy functions that are ready made for filter hooks, including the one I use above. When all you need is a function that returns one of these simple values, no matter what, then it’s best to use one of these — it means one less function to define in your own code, and thus one less thing to document and test! Here’s the full set:

So for simple jobs like this one, job can be done with a single well-placed filter hook.