The months_dropdown_results WordPress PHP filter allows you to modify the months drop-down results displayed in the admin area when filtering posts by date.
Usage
add_filter('months_dropdown_results', 'my_custom_months_dropdown_results', 10, 2); function my_custom_months_dropdown_results($months, $post_type) { // your custom code here return $months; }
Parameters
- $months (object[]) – An array of the months drop-down query results.
- $post_type (string) – The post type.
More information
See WordPress Developer Resources: months_dropdown_results
Examples
Change Months Dropdown Order
Reverse the order of the months in the drop-down list.
add_filter('months_dropdown_results', 'reverse_months_dropdown_order', 10, 2); function reverse_months_dropdown_order($months, $post_type) { return array_reverse($months); }
Exclude a Specific Month
Exclude a specific month from the drop-down list, for example, January 2023.
add_filter('months_dropdown_results', 'exclude_specific_month', 10, 2); function exclude_specific_month($months, $post_type) { return array_filter($months, function($month) { return $month->year != 2023 || $month->month != 1; }); }
Limit Months Dropdown to Recent Months
Limit the months drop-down to show only the last 6 months.
add_filter('months_dropdown_results', 'limit_months_dropdown', 10, 2); function limit_months_dropdown($months, $post_type) { return array_slice($months, 0, 6); }
Add Custom Option to Months Dropdown
Add a custom option to the months drop-down list, for example, “Last 30 Days.”
add_filter('months_dropdown_results', 'add_custom_option', 10, 2); function add_custom_option($months, $post_type) { $custom_option = new stdClass; $custom_option->year = 0; $custom_option->month = 0; $custom_option->text = __('Last 30 Days'); array_unshift($months, $custom_option); return $months; }
Remove Months Dropdown for a Specific Post Type
Remove the months drop-down for a specific post type, for example, “product.”
add_filter('months_dropdown_results', 'remove_months_dropdown_for_post_type', 10, 2); function remove_months_dropdown_for_post_type($months, $post_type) { if ($post_type == 'product') { return array(); } return $months; }