How to Remove the Date Filter on Post Type Admin Pages in WordPress

Home » Snippets » How to Remove the Date Filter on Post Type Admin Pages in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

In WordPress, the admin dashboard allows you to easily manage your content, including posts and custom post types. One of the default features of the posts list page is the ability to filter your posts by date. However, in some cases, you may want to remove this filter completely, either for aesthetic purposes or to simplify the interface for your users. In this article, we’ll explore different methods for removing the date filter from your post type admin pages.

					function wpturbo_remove_date_filter( $post_type ) {
    if ( $post_type == 'my_post_type' ) { // replace 'my_post_type' with your actual post type
        global $wp_filter;
        foreach ( (array) $wp_filter['months_dropdown_results'] as $key => $value ) {
            if ( preg_match( "/post_type=($post_type)/", implode( ' ', array_keys( $value ) ) ) ) {
                unset( $wp_filter['months_dropdown_results'][$key] );
                break;
            }
        }
    }
}
add_action( 'restrict_manage_posts', 'wpturbo_remove_date_filter' );
				

The code snippet above shows how to remove the date filter on a specific post type admin page in WordPress. This is useful if you don’t want users to filter posts by date within a certain post type.

To use this code, you’ll need to replace the ‘my_post_type’ value in the function with the actual post type you want to remove the date filter from.

Next, we define a new function called wpturbo_remove_date_filter() that accepts a $post_type argument. Inside the function, we check if the $post_type is equal to the post type we want to remove the date filter from.

If the condition is true, we use the global $wp_filter statement to get all the filters that are currently hooked into the months_dropdown_results hook. We loop through these filters and use a regular expression to match the post type we want to remove the date filter from. Once we find a match, we use the unset() function to remove the filter from the $wp_filter array.

The final step is to hook the wpturbo_remove_date_filter() function into the restrict_manage_posts action. This ensures that the function is called whenever the post type admin page is loaded and the date filter is displayed.

Register an account to save your snippets or go Pro to get more features.