wppaste
WordPress

WP_List_Table::months_dropdown( string $post_type )

Since
3.1.0
Source
wp-admin/includes/class-wp-list-table.php:712
Displays a dropdown for filtering items in the list table by month.

Compatibility

WordPress
since 3.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_typestring
The post type.

Performance profile

How much work a call to WP_List_Table::months_dropdown() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_results().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
10–95

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 144.

Plugin surface
3 hooks

Third-party callbacks on 'disable_months_dropdown', 'pre_months_dropdown_query', 'months_dropdown_results' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • sqldatabase query->get_results()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 7 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_List_Table::months_dropdown() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
apply_filters()10apply_filters()
!apply_filters() && is_array($months)26–33apply_filters(), apply_filters(), apply_filters()
!apply_filters() && !is_array($months)46–58apply_filters(), apply_filters(), ->prepare(), ->get_results(), apply_filters()
!apply_filters() && is_array($months) && $months53–62apply_filters(), apply_filters(), apply_filters(), get_post_type_object(), selected(), _e()
!apply_filters() && !is_array($months) && isset($value)59–66apply_filters(), apply_filters(), ->prepare(), ->prepare(), ->get_results(), apply_filters()
!apply_filters() && !is_array($months) && $months73–87apply_filters(), apply_filters(), ->prepare(), ->get_results(), apply_filters(), get_post_type_object(), selected(), _e()
!apply_filters() && !is_array($months) && isset($value) && $months86–95apply_filters(), apply_filters(), ->prepare(), ->prepare(), ->get_results(), apply_filters(), get_post_type_object(), selected(), _e()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14610–95122 more instructions than PHP 8.5
8.514410–9512
8.414410–9512
8.314410–9512
8.214410–9512
8.114410–95121 fewer instruction than PHP 7.4
7.414510–9612

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while WP_List_Table::months_dropdown() runs, in this order:

  1. apply_filters( disable_months_dropdown )filterline 723 (+11 into the body)

    Filters whether to remove the 'Months' drop-down from the post list table.

  2. apply_filters( pre_months_dropdown_query )filterline 735 (+23 into the body)

    Filters whether to short-circuit performing the months dropdown query.

  3. apply_filters( months_dropdown_results )filterline 765 (+53 into the body)

    Filters the 'Months' drop-down results.

Uses · 8

Source code

	protected function months_dropdown( $post_type ) {		global $wpdb, $wp_locale; 		/**		 * Filters whether to remove the 'Months' drop-down from the post list table.		 *		 * @since 4.2.0		 *		 * @param bool   $disable   Whether to disable the drop-down. Default false.		 * @param string $post_type The post type.		 */		if ( apply_filters( 'disable_months_dropdown', false, $post_type ) ) {			return;		} 		/**		 * Filters whether to short-circuit performing the months dropdown query.		 *		 * @since 5.7.0		 *		 * @param object[]|false $months   'Months' drop-down results. Default false.		 * @param string         $post_type The post type.		 */		$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				)			);		} 		/**		 * Filters the 'Months' drop-down results.		 *		 * @since 3.7.0		 *		 * @param object[] $months    Array of the months drop-down query results.		 * @param string   $post_type The post type.		 */		$months = apply_filters( 'months_dropdown_results', $months, $post_type ); 		$month_count = count( $months ); 		if ( ! $month_count || ( 1 === $month_count && 0 === (int) $months[0]->month ) ) {			return;		} 		$selected_month = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;		?>		<label for="filter-by-date" class="screen-reader-text"><?php echo get_post_type_object( $post_type )->labels->filter_by_date; ?></label>		<select name="m" id="filter-by-date">			<option<?php selected( $selected_month, 0 ); ?> value="0"><?php _e( 'All dates' ); ?></option>		<?php		foreach ( $months as $arc_row ) {			if ( 0 === (int) $arc_row->year ) {				continue;			} 			$month = zeroise( $arc_row->month, 2 );			$year  = $arc_row->year; 			printf(				"<option %s value='%s'>%s</option>\n",				selected( $selected_month, $year . $month, false ),				esc_attr( $year . $month ),				/* translators: 1: Month name, 2: 4-digit year. */

Changelog

Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-admin/includes/class-wp-list-table.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.