get_adjacent_post_rel_link( string $title = '%title', bool $in_same_term = false, int[]|string $excluded_terms = '', bool $previous = true, string $taxonomy = 'category' ): string|null
- Since
- 2.8.0
- Source
wp-includes/link-template.php:2061
Description
Can either be next or previous post relational link.
Compatibility
- WordPress
- since 2.8.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
$titlestringoptional- Link title format. Default '%title'.Default:
'%title' $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 term 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|null- The adjacent post relational link URL.
Performance profile
How much work a call to get_adjacent_post_rel_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
- 19–77
- Plugin surface
- 1 hook
- 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 91.
Third-party callbacks on '{$adjacent}_post_rel_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 - optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get_last_changed()one call below get_adjacent_post_rel_link() - serializeserialisation
maybe_unserialize()one call below get_adjacent_post_rel_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_rel_link() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($post) | 19 | get_post(), get_adjacent_post() |
empty($post) | 22–23 | get_post(), is_attachment(), get_adjacent_post() |
is_attachment() && empty($post) | 23 | get_post(), is_attachment(), get_post() |
!empty($post) && !empty($post_title) | 64–66 | get_post(), get_adjacent_post(), echo(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters() |
!empty($post) && !empty($post_title) | 67–70 | get_post(), is_attachment(), get_adjacent_post(), echo(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters() |
is_attachment() && !empty($post) && !empty($post_title) | 68–70 | get_post(), is_attachment(), get_post(), echo(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters() |
!empty($post) && empty($post_title) | 70–73 | get_post(), get_adjacent_post(), echo(), __(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters() |
!empty($post) && empty($post_title) | 73–77 | get_post(), is_attachment(), get_adjacent_post(), echo(), __(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters() |
is_attachment() && !empty($post) && empty($post_title) | 74–77 | get_post(), is_attachment(), get_post(), echo(), __(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 89 | 19–76 | 8 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 91 | 19–77 | 8 | |
| 8.4 | 91 | 19–77 | 8 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 97 | 19–83 | 8 | |
| 8.2 | 97 | 19–83 | 8 | |
| 8.1 | 97 | 19–83 | 8 | |
| 7.4 | 97 | 19–83 | 8 |
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 · 1
One hook fires while get_adjacent_post_rel_link() runs, in this order:
- apply_filters( {$adjacent}_post_rel_link )filterline 2110 (+49 into the body)
Filters the adjacent post relational link.
Uses · 10
- get_post()Retrieves post data given a post ID or post object.
- is_attachment()Determines whether the query is for an existing attachment page.
- get_adjacent_post()Retrieves the adjacent post.
- the_title_attribute()Sanitizes the current title when retrieving or displaying.
- __()Retrieves the translation of $text.
- mysql2date()Converts given MySQL date string into a different format.
- get_option()Retrieves an option value based on an option name.
- esc_attr()Escaping for HTML attributes.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 3
- adjacent_posts_rel_link()Displays the relational links for the posts adjacent to the current post.
- next_post_rel_link()Displays the relational link for the next post adjacent to the current post.
- prev_post_rel_link()Displays the relational link for the previous post adjacent to the current post.
Source code
function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { $post = get_post(); if ( $previous && is_attachment() && $post ) { $post = get_post( $post->post_parent ); } else { $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); } if ( empty( $post ) ) { return null; } $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post, ) ); if ( empty( $post_title ) ) { $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); } $date = mysql2date( get_option( 'date_format' ), $post->post_date ); $title = str_replace( '%title', $post_title, $title ); $title = str_replace( '%date', $date, $title ); $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; $link .= esc_attr( $title ); $link .= "' href='" . get_permalink( $post ) . "' />\n"; $adjacent = $previous ? 'previous' : 'next'; /** * Filters the adjacent post relational link. * * The dynamic portion of the hook name, `$adjacent`, refers to the type * of adjacency, 'next' or 'previous'. * * Possible hook names include: * * - `next_post_rel_link` * - `previous_post_rel_link` * * @since 2.8.0 * * @param string $link The relational link. */ return apply_filters( "{$adjacent}_post_rel_link", $link );}Changelog
Introduced in 2.8.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
string|void to string|null.verified against sourceAbout 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.