get_adjacent_post_link( string $format, string $link, bool $in_same_term = false, int[]|string $excluded_terms = '', bool $previous = true, string $taxonomy = 'category' ): string
- Since
- 3.7.0
- Source
wp-includes/link-template.php:2344
Description
Can be either next post link or previous.
Compatibility
- WordPress
- since 3.7.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
$formatstring- Link anchor format.
$linkstring- Link permalink format.
$in_same_termbooloptional- Whether link should be in the same taxonomy term.
Default false.Default:false $excluded_termsint[]|stringoptional- Array or comma-separated list of excluded terms IDs.
Default empty.Default:'' $previousbooloptional- Whether to display link to previous or next post.
Default true.Default:true $taxonomystringoptional- Taxonomy, if
$in_same_termis true. Default 'category'.Default:'category'
Return value
string- The link URL of the previous or next post in relation to the current post.
Performance profile
How much work a call to get_adjacent_post_link() 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
- Constant
- Instructions
- 30–83
- Plugin surface
- 2 hooks
- Called by
- 3
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 98.
Third-party callbacks on 'the_title', '{$adjacent}_post_link' 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
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - cacheobject cache
wp_cache_get_last_changed()one call below get_adjacent_post_link() - serializeserialisation
maybe_unserialize()one call below get_adjacent_post_link()
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 · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_adjacent_post_link() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 30–31 | get_adjacent_post(), apply_filters() |
!is_attachment() | 33–34 | is_attachment(), get_adjacent_post(), apply_filters() |
is_attachment() | 35–36 | is_attachment(), get_post(), get_post(), apply_filters() |
!empty($post) | 69–71 | get_adjacent_post(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters() |
!is_attachment() && !empty($post) | 72–74 | is_attachment(), get_adjacent_post(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters() |
is_attachment() && !empty($post) | 74–76 | is_attachment(), get_post(), get_post(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters() |
empty($post) | 75–78 | get_adjacent_post(), __(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters() |
!is_attachment() && empty($post) | 78–81 | is_attachment(), get_adjacent_post(), __(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters() |
is_attachment() && empty($post) | 80–83 | is_attachment(), get_post(), get_post(), __(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 96 | 30–82 | 7 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 98 | 30–83 | 7 | |
| 8.4 | 98 | 30–83 | 7 | 11 fewer instructions than PHP 8.3 |
| 8.3 | 109 | 30–94 | 7 | |
| 8.2 | 109 | 30–94 | 7 | |
| 8.1 | 109 | 30–94 | 7 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 110 | 30–95 | 7 |
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 · 2
2 hooks fire while get_adjacent_post_link() runs, in this order:
- apply_filters( {$adjacent}_post_link )filterline 2396 (+52 into the body)
Filters the adjacent post link.
Uses · 8
- is_attachment()Determines whether the query is for an existing attachment page.
- get_post()Retrieves post data given a post ID or post object.
- get_adjacent_post()Retrieves the adjacent post.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- mysql2date()Converts given MySQL date string into a different format.
- get_option()Retrieves an option value based on an option name.
- get_permalink()Retrieves the full permalink for the current post or post ID.
Used by · 3
- adjacent_post_link()Displays the adjacent post link.
- get_next_post_link()Retrieves the next post link that is adjacent to the current post.
- get_previous_post_link()Retrieves the previous post link that is adjacent to the current post.
Source code
function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { if ( $previous && is_attachment() ) { $post = get_post( get_post()->post_parent ); } else { $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); } if ( ! $post ) { $output = ''; } else { $title = $post->post_title; if ( empty( $post->post_title ) ) { $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); } /** This filter is documented in wp-includes/post-template.php */ $title = apply_filters( 'the_title', $title, $post->ID ); $date = mysql2date( get_option( 'date_format' ), $post->post_date ); $rel = $previous ? 'prev' : 'next'; $string = '<a href="' . get_permalink( $post ) . '" rel="' . $rel . '">'; $inlink = str_replace( '%title', $title, $link ); $inlink = str_replace( '%date', $date, $inlink ); $inlink = $string . $inlink . '</a>'; $output = str_replace( '%link', $inlink, $format ); } $adjacent = $previous ? 'previous' : 'next'; /** * Filters the adjacent post link. * * The dynamic portion of the hook name, `$adjacent`, refers to the type * of adjacency, 'next' or 'previous'. * * Possible hook names include: * * - `next_post_link` * - `previous_post_link` * * @since 2.6.0 * @since 4.2.0 Added the `$adjacent` parameter. * * @param string $output The adjacent post link. * @param string $format Link anchor format. * @param string $link Link permalink format. * @param WP_Post|string $post The adjacent post. Empty string if no corresponding post exists. * @param string $adjacent Whether the post is previous or next. */ return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );}Changelog
Introduced in 3.7.0. Unchanged from 6.7.7 through 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-includes/link-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.