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.

Facebooktwittergoogle_plusredditlinkedinmailFacebooktwittergoogle_plusredditlinkedinmail
  • codeview

    Excatly what I needed…but how do I get rid of the filter button too?

    • I can’t see an easy way to remove that button. It’s called from the method WP_Posts_List_Table::extra_tablenav() in wp-admin/includes/class-wp-posts-list-table.php, and there doesn’t seem to be any way to intercept that submit_button() call directly.

      cheers,
      Ross

      • codeview

        I went with a CSS hide option, added this in my functions.php:

        function add_admin_styles() {

        echo ‘

        #posts-filter .tablenav select[name=m], /* Dates Dropdown (with below) */

        #posts-filter .tablenav select[name=cat], /* Dates Dropdown (with above) */

        #posts-filter .tablenav #post-query-submit /* Filter button */

        {display:none;}

        ‘;

        }

        add_action(‘admin_head’, ‘add_admin_styles’);

  • thanks, very useful :)

  • Mike D

    Very cool, Ross. Thanks for the post.

    I actually stumbled across a different hook which might work even better: ‘disable_months_dropdown’. This should accomplish the same effect, but in theory, should be slightly more efficient, since I believe that it can cause WP to skip querying for the months to put into the dropdown, rather than querying for them but not displaying them.