Using WordPress ‘disable_months_dropdown’ PHP filter

The disable_months_dropdown WordPress PHP filter allows you to control the visibility of the ‘Months’ drop-down in the post list table.

Usage

add_filter( 'disable_months_dropdown', 'my_custom_function', 10, 2 );

function my_custom_function( $disable, $post_type ) {
    // your custom code here
    return $disable;
}

Parameters

  • $disable (bool) – Whether to disable the drop-down. Default false.
  • $post_type (string) – The post type.

More information

See WordPress Developer Resources: disable_months_dropdown

Examples

Hide ‘Months’ drop-down for all post types

This example hides the ‘Months’ drop-down for all post types in the post list table.

add_filter( 'disable_months_dropdown', 'hide_months_dropdown_all_post_types', 10, 2 );

function hide_months_dropdown_all_post_types( $disable, $post_type ) {
    return true;
}

Hide ‘Months’ drop-down for specific post types

This example hides the ‘Months’ drop-down for the ‘product’ and ‘testimonial’ post types.

add_filter( 'disable_months_dropdown', 'hide_months_dropdown_specific_post_types', 10, 2 );

function hide_months_dropdown_specific_post_types( $disable, $post_type ) {
    $post_types_to_hide = array( 'product', 'testimonial' );

    if ( in_array( $post_type, $post_types_to_hide ) ) {
        return true;
    }

    return $disable;
}

Show ‘Months’ drop-down only for specific post types

This example shows the ‘Months’ drop-down only for the ‘post’ and ‘page’ post types.

add_filter( 'disable_months_dropdown', 'show_months_dropdown_specific_post_types', 10, 2 );

function show_months_dropdown_specific_post_types( $disable, $post_type ) {
    $post_types_to_show = array( 'post', 'page' );

    if ( !in_array( $post_type, $post_types_to_show ) ) {
        return true;
    }

    return $disable;
}

Hide ‘Months’ drop-down for custom post types

This example hides the ‘Months’ drop-down for all custom post types.

add_filter( 'disable_months_dropdown', 'hide_months_dropdown_custom_post_types', 10, 2 );

function hide_months_dropdown_custom_post_types( $disable, $post_type ) {
    if ( 'post' !== $post_type && 'page' !== $post_type ) {
        return true;
    }

    return $disable;
}

Show ‘Months’ drop-down based on user role

This example shows the ‘Months’ drop-down only for users with the ‘editor’ role.

add_filter( 'disable_months_dropdown', 'show_months_dropdown_based_on_user_role', 10, 2 );

function show_months_dropdown_based_on_user_role( $disable, $post_type ) {
    $user = wp_get_current_user();

    if ( !in_array( 'editor', $user->roles ) ) {
        return true;
    }

    return $disable;
}