How add custom drop down filter in WordPress wp_list_table?
WP_List_Table months_dropdown:
WP_List_Table::months_dropdown(string $post_type) // It displays a dropdown for filtering items in the list table by month.
Full Source Code:
File: wp-admin/includes/class-wp-list-table.php
protected function months_dropdown( $post_type ) { global $wpdb, $wp_locale; if ( apply_filters( 'disable_months_dropdown', false, $post_type ) ) { return; } $months = apply_filters( 'pre_months_dropdown_query', false, $post_type ); if ( ! is_array( $months ) ) { $extra_checks = "AND post_status != 'auto-draft'"; if ( ! isset( $_GET['post_status'] ) || 'trash' !== $_GET['post_status'] ) { $extra_checks .= " AND post_status != 'trash'"; } elseif ( isset( $_GET['post_status'] ) ) { $extra_checks = $wpdb->prepare( ' AND post_status = %s', $_GET['post_status'] ); } $months = $wpdb->get_results( $wpdb->prepare( " SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month FROM $wpdb->posts WHERE post_type = %s $extra_checks ORDER BY post_date DESC ", $post_type ) ); } $months = apply_filters( 'months_dropdown_results', $months, $post_type ); $month_count = count( $months ); if ( ! $month_count || ( 1 == $month_count && 0 == $months[0]->month ) ) { return; } $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0; ?> $month = zeroise( $arc_row->month, 2 ); $year = $arc_row->year; printf( "%s\n", selected( $m, $year . $month, false ), esc_attr( $arc_row->year . $month ), /* translators: 1: Month name, 2: 4-digit year. */ sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year ) ); } ?> }