wppaste
WordPress

get_calendar( bool $initial = true, bool $display = true ): void|string

Since
1.0.0
Source
wp-includes/general-template.php:2248
Displays calendar with days that have posts as links.

Description

The calendar is cached, which will be retrieved, if it exists. If there are no posts for the month, then it will not be displayed.

Compatibility

WordPress
since 1.0.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

$initialbooloptional
Whether to use initial calendar names. Default true.Default: true
$displaybooloptional
Whether to display the calendar output. Default true.Default: true

Return value

void|string
Void if $display argument is true, calendar HTML if $display is false.

Performance profile

How much work a call to get_calendar() 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_row().

Scaling
Scales with input

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

Instructions
200–268

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

Plugin surface
1 hook

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

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_get()called directly
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • sqldatabase query->get_row()called directly
  • serializeserialisationmaybe_unserialize()one call below get_calendar()

Further down the call graph this can also reach 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 · 24 distinct outcomes

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

WhenInstructionsCalls it makes
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad == 0 && !$day200–208md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad != 0 && !$day206–214md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad == 0 && !$day207–214md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad == 0 && !$day && $pad != 0 && $pad != 7208–214md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad != 0 && !$day213–220md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad != 0 && !$day && $pad != 7214–220md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad == 0 && !$day && $pad != 0 && $pad != 7215–220md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad != 0 && !$day && $pad != 7221–226md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad == 0 && !$day221–229md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad != 0 && !$day227–235md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad == 0 && !$day228–235md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad == 0 && !$day && $pad != 0 && $pad != 7229–235md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
12 further outcomes, up to 268 instructions
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad != 0 && !$day234–241md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad != 0 && !$day && $pad != 7235–241md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad == 0 && !$day && $pad != 0 && $pad != 7236–241md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad != 0 && !$day && $pad != 7242–247md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad == 0 && !$day242–250md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad != 0 && !$day248–256md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad == 0 && !$day249–256md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad == 0 && !$day && $pad != 0 && $pad != 7250–256md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad != 0 && !$day255–262md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad != 0 && !$day && $pad != 7256–262md5(), wp_cache_get(), get_option(), current_time(), current_time(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad == 0 && !$day && $pad != 0 && $pad != 7257–262md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && !empty($m) && !$wdcount && $pad != 0 && !$day && $pad != 7263–268md5(), wp_cache_get(), get_option(), zeroise(), mktime(), gmdate(), ->get_row(), ->get_row(), _x(), ->get_month(), gmdate(), sprintf(), ->get_results(), gmdate(), calendar_week_mod(), esc_attr(), gmdate(), mktime(), gmdate(), calendar_week_mod(), esc_attr(), __(), get_month_link(), ->get_month(), ->get_month_abbrev(), get_month_link(), ->get_month(), ->get_month_abbrev(), wp_cache_set(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev471200–268342 fewer instructions than PHP 8.5
8.5473200–26834
8.4473200–2683411 fewer instructions than PHP 8.3
8.3484200–27434
8.2484200–27434
8.1484200–27434
7.4484200–27434

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 get_calendar() runs, in this order:

  1. apply_filters( get_calendar )filterline 2256 (+8 into the body)

    Filters the HTML calendar output.

  2. apply_filters( get_calendar )filterline 2463 (+215 into the body)

    Filters the HTML calendar output.

  3. apply_filters( get_calendar )filterline 2467 (+219 into the body)

    Filters the HTML calendar output.

Uses · 12

Used by · 2

Source code

function get_calendar( $initial = true, $display = true ) {	global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; 	$key   = md5( $m . $monthnum . $year );	$cache = wp_cache_get( 'get_calendar', 'calendar' ); 	if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {		/** This filter is documented in wp-includes/general-template.php */		$output = apply_filters( 'get_calendar', $cache[ $key ] ); 		if ( $display ) {			echo $output;			return;		} 		return $output;	} 	if ( ! is_array( $cache ) ) {		$cache = array();	} 	// Quick check. If we have no posts at all, abort!	if ( ! $posts ) {		$gotsome = $wpdb->get_var( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1" );		if ( ! $gotsome ) {			$cache[ $key ] = '';			wp_cache_set( 'get_calendar', $cache, 'calendar' );			return;		}	} 	if ( isset( $_GET['w'] ) ) {		$w = (int) $_GET['w'];	}	// week_begins = 0 stands for Sunday.	$week_begins = (int) get_option( 'start_of_week' ); 	// Let's figure out when we are.	if ( ! empty( $monthnum ) && ! empty( $year ) ) {		$thismonth = zeroise( (int) $monthnum, 2 );		$thisyear  = (int) $year;	} elseif ( ! empty( $w ) ) {		// We need to get the month from MySQL.		$thisyear = (int) substr( $m, 0, 4 );		// It seems MySQL's weeks disagree with PHP's.		$d         = ( ( $w - 1 ) * 7 ) + 6;		$thismonth = $wpdb->get_var( "SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')" );	} elseif ( ! empty( $m ) ) {		$thisyear = (int) substr( $m, 0, 4 );		if ( strlen( $m ) < 6 ) {			$thismonth = '01';		} else {			$thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );		}	} else {		$thisyear  = current_time( 'Y' );		$thismonth = current_time( 'm' );	} 	$unixmonth = mktime( 0, 0, 0, $thismonth, 1, $thisyear );	$last_day  = gmdate( 't', $unixmonth ); 	// Get the next and previous month and year with at least one post.	$previous = $wpdb->get_row(		"SELECT MONTH(post_date) AS month, YEAR(post_date) AS year		FROM $wpdb->posts		WHERE post_date < '$thisyear-$thismonth-01'		AND post_type = 'post' AND post_status = 'publish'		ORDER BY post_date DESC		LIMIT 1"	);	$next     = $wpdb->get_row(		"SELECT MONTH(post_date) AS month, YEAR(post_date) AS year		FROM $wpdb->posts		WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'		AND post_type = 'post' AND post_status = 'publish'		ORDER BY post_date ASC		LIMIT 1"	);

Changelog

Introduced in 1.0.0. 3 changes between 6.7.7 and 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.

6.8.8
Parameter $args added.verified against source
6.8.8
Parameter $initial removed.verified against source
6.8.8
Parameter $display removed.verified against source
1.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/general-template.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.