get_calendar( bool $initial = true, bool $display = true ): void|string
- Since
- 1.0.0
- Source
wp-includes/general-template.php:2248
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
$displayargument is true, calendar HTML if$displayis 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
- Scaling
- Scales with input
- Instructions
- 200–268
- Plugin surface
- 1 hook
- Called by
- 2
Reaches the database via ->get_row().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 473.
Third-party callbacks on 'get_calendar' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - sqldatabase query
->get_row()called directly - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
is_array($cache) && !isset($value) && empty($monthnum) && empty($w) && empty($m) && !$wdcount && $pad == 0 && !$day | 200–208 | md5(), 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 && !$day | 206–214 | md5(), 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 && !$day | 207–214 | md5(), 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 != 7 | 208–214 | md5(), 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 && !$day | 213–220 | md5(), 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 != 7 | 214–220 | md5(), 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 != 7 | 215–220 | md5(), 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 != 7 | 221–226 | md5(), 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 && !$day | 221–229 | md5(), 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 && !$day | 227–235 | md5(), 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 && !$day | 228–235 | md5(), 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 != 7 | 229–235 | md5(), 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 && !$day | 234–241 | md5(), 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 != 7 | 235–241 | md5(), 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 != 7 | 236–241 | md5(), 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 != 7 | 242–247 | md5(), 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 && !$day | 242–250 | md5(), 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 && !$day | 248–256 | md5(), 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 && !$day | 249–256 | md5(), 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 != 7 | 250–256 | md5(), 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 && !$day | 255–262 | md5(), 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 != 7 | 256–262 | md5(), 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 != 7 | 257–262 | md5(), 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 != 7 | 263–268 | md5(), 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 471 | 200–268 | 34 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 473 | 200–268 | 34 | |
| 8.4 | 473 | 200–268 | 34 | 11 fewer instructions than PHP 8.3 |
| 8.3 | 484 | 200–274 | 34 | |
| 8.2 | 484 | 200–274 | 34 | |
| 8.1 | 484 | 200–274 | 34 | |
| 7.4 | 484 | 200–274 | 34 |
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:
Uses · 12
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_cache_set()Saves the data to the cache.
- get_option()Retrieves an option value based on an option name.
- zeroise()Add leading zeros when necessary.
- current_time()Retrieves the current time based on specified type.
- _x()Retrieves translated string with gettext context.
- esc_attr()Escaping for HTML attributes.
- calendar_week_mod()Gets number of days since the start of the week.
- __()Retrieves the translation of $text.
- get_day_link()Retrieves the permalink for the day archives with year and month.
- get_month_link()Retrieves the permalink for the month archives with year.
Used by · 2
- WP_Widget_Calendar::widget()Outputs the content for the current Calendar widget instance.
- render_block_core_calendar()Renders the `core/calendar` block on server.
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.
Signature, return type and hooks compared across 5 parsed releases.
$args added.verified against source$initial removed.verified against source$display removed.verified against sourceAbout 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.