wp_get_archives( string|array $args = '' ): void|string
- Since
- 1.2.0, 4.4.0, 5.2.0
- Source
wp-includes/general-template.php:1996
Compatibility
- WordPress
- since 5.2.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
$argsstring|arrayoptional- Default archive links arguments. Optional.Default:
''$typestringdefault: 'monthly'Type of archive to retrieve. Accepts 'daily', 'weekly', 'monthly', 'yearly', 'postbypost', or 'alpha'. Both 'postbypost' and 'alpha' display the same archive link list as well as post titles instead of displaying dates. The difference between the two is that 'alpha' will order by post title and 'postbypost' will order by post date.$limitstring|intdefault: empty (no limit)Number of links to limit the query to.$formatstringdefault: 'html'Format each link should take using the $before and $after args. Accepts 'link' (<link>tag), 'option' (<option>tag), 'html' (<li>tag), or a custom format, which generates a link anchor with $before preceding and $after succeeding.$beforestringdefault: emptyMarkup to prepend to the beginning of each link.$afterstringdefault: emptyMarkup to append to the end of each link.$show_post_countbooldefault: falseWhether to display the post count alongside the link.$echobool|intdefault: 1|true to echoWhether to echo or return the links list.$orderstringdefault: 'DESC'Whether to use ascending or descending order. Accepts 'ASC', or 'DESC'.$post_typestringdefault: 'post'Post type.$yearstringdefault: current yearYear.$monthnumstringdefault: current month numberMonth number.$daystringdefault: current dayDay.$wstringdefault: current weekWeek.
Return value
void|string- Void if 'echo' argument is true, archive links if 'echo' is false.
Performance profile
How much work a call to wp_get_archives() 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
- 105–164
- Plugin surface
- 3 hooks
- Called by
- 3
Reaches the database via ->get_results().
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 699.
Third-party callbacks on 'getarchives_where', 'getarchives_join', 'the_title' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get_last_changed()called directly - optionoption read or write
get_option()called directly - sqldatabase query
->get_results()called directly - serializeserialisation
maybe_unserialize()one call below wp_get_archives() - querycontent query
get_post()one call below wp_get_archives()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_archives() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_post_type_viewable() && empty($parsed_args) | 105–109 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed() |
is_post_type_viewable() && !empty($parsed_args) | 116–118 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), absint(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed() |
is_post_type_viewable() && empty($parsed_args) | 121–145 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed(), md5(), wp_cache_get() |
is_post_type_viewable() && empty($parsed_args) | 126–155 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed(), md5(), wp_cache_get(), ->get_results(), wp_cache_set() |
is_post_type_viewable() && !empty($parsed_args) | 132–154 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), absint(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed(), md5(), wp_cache_get() |
is_post_type_viewable() && empty($parsed_args) | 135–145 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed(), _wp_mysql_week(), md5(), wp_cache_get() |
is_post_type_viewable() && !empty($parsed_args) | 137–164 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), absint(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed(), md5(), wp_cache_get(), ->get_results(), wp_cache_set() |
is_post_type_viewable() && empty($parsed_args) | 144–154 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed(), _wp_mysql_week(), md5(), wp_cache_get(), ->get_results(), wp_cache_set() |
is_post_type_viewable() && !empty($parsed_args) | 146–154 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), absint(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed(), _wp_mysql_week(), md5(), wp_cache_get() |
is_post_type_viewable() && !empty($parsed_args) | 155–163 | get_query_var(), get_query_var(), get_query_var(), get_query_var(), wp_parse_args(), get_post_type_object(), is_post_type_viewable(), absint(), strtoupper(), ->prepare(), apply_filters(), apply_filters(), wp_cache_get_last_changed(), _wp_mysql_week(), md5(), wp_cache_get(), ->get_results(), wp_cache_set() |
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 | 699 | 105–164 | 51 | |
| 8.5 | 699 | 105–164 | 51 | |
| 8.4 | 699 | 105–164 | 51 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 701 | 105–164 | 51 | |
| 8.2 | 701 | 105–164 | 51 | |
| 8.1 | 701 | 105–164 | 51 | |
| 7.4 | 701 | 105–164 | 51 |
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_get_archives() runs, in this order:
- apply_filters( getarchives_where )filterline 2051 (+55 into the body)
Filters the SQL WHERE clause for retrieving archives.
- apply_filters( getarchives_join )filterline 2061 (+65 into the body)
Filters the SQL JOIN clause for retrieving archives.
Uses · 24
- get_query_var()Retrieves the value of a query variable in the WP_Query class.
- wp_parse_args()Merges user defined arguments into defaults array.
- get_post_type_object()Retrieves a post type object by name.
- is_post_type_viewable()Determines whether a post type is considered "viewable".
- absint()Converts a value to non-negative integer.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_cache_get_last_changed()Gets last changed date for the specified cache group.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- wp_cache_set()Saves the data to the cache.
- get_month_link()Retrieves the permalink for the month archives with year.
- add_query_arg()Retrieves a modified URL query string.
- __()Retrieves the translation of $text.
Show all 24
- is_archive()Determines whether the query is for an existing archive page.
- get_archives_link()Retrieves archive link content based on predefined or custom code.
- get_year_link()Retrieves the permalink for the year archives.
- get_day_link()Retrieves the permalink for the day archives with year and month.
- mysql2date()Converts given MySQL date string into a different format.
- get_option()Retrieves an option value based on an option name.
- _wp_mysql_week()Returns a MySQL expression for selecting the week number based on the start_of_week option.
- get_weekstartend()Gets the week start and end from the datetime or date string from MySQL.
- date_i18n()Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- get_the_ID()Retrieves the ID of the current item in the WordPress Loop.
Used by · 3
- WP_Widget_Archives::widget()Outputs the content for the current Archives widget instance.
- get_archives()Retrieves a list of archives.
- render_block_core_archives()Renders the `core/archives` block on server.
Source code
function wp_get_archives( $args = '' ) { global $wpdb, $wp_locale; $defaults = array( 'type' => 'monthly', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1, 'order' => 'DESC', 'post_type' => 'post', 'year' => get_query_var( 'year' ), 'monthnum' => get_query_var( 'monthnum' ), 'day' => get_query_var( 'day' ), 'w' => get_query_var( 'w' ), ); $parsed_args = wp_parse_args( $args, $defaults ); $post_type_object = get_post_type_object( $parsed_args['post_type'] ); if ( ! is_post_type_viewable( $post_type_object ) ) { return; } $parsed_args['post_type'] = $post_type_object->name; if ( '' === $parsed_args['type'] ) { $parsed_args['type'] = 'monthly'; } if ( ! empty( $parsed_args['limit'] ) ) { $parsed_args['limit'] = absint( $parsed_args['limit'] ); $parsed_args['limit'] = ' LIMIT ' . $parsed_args['limit']; } $order = strtoupper( $parsed_args['order'] ); if ( 'ASC' !== $order ) { $order = 'DESC'; } // This is what will separate dates on weekly archive links. $archive_week_separator = '–'; $sql_where = $wpdb->prepare( "WHERE post_type = %s AND post_status = 'publish'", $parsed_args['post_type'] ); /** * Filters the SQL WHERE clause for retrieving archives. * * @since 2.2.0 * * @param string $sql_where Portion of SQL query containing the WHERE clause. * @param array $parsed_args An array of default arguments. */ $where = apply_filters( 'getarchives_where', $sql_where, $parsed_args ); /** * Filters the SQL JOIN clause for retrieving archives. * * @since 2.2.0 * * @param string $sql_join Portion of SQL query containing JOIN clause. * @param array $parsed_args An array of default arguments. */ $join = apply_filters( 'getarchives_join', '', $parsed_args ); $output = ''; $last_changed = wp_cache_get_last_changed( 'posts' ); $limit = $parsed_args['limit']; if ( 'monthly' === $parsed_args['type'] ) { $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit"; $key = md5( $query ); $key = "wp_get_archives:$key:$last_changed"; $results = wp_cache_get( $key, 'post-queries' ); if ( ! $results ) { $results = $wpdb->get_results( $query );Changelog
Introduced in 1.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$year, $monthnum, $day, and $w arguments were added.from the docblock$post_type argument was added.from the docblockAbout 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.