wppaste
WordPress

get_adjacent_post_rel_link( string $title = '%title', bool $in_same_term = false, int[]|string $excluded_terms = '', bool $previous = true, string $taxonomy = 'category' ): string|void

Since
2.8.0
Source
wp-includes/link-template.php:2053
Retrieves the adjacent post relational link.

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_term is true. Default 'category'.Default: 'category'

Return value

string|void
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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
19–77

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

Plugin surface
1 hook

Third-party callbacks on '{$adjacent}_post_rel_link' run inside this call, and their cost is not bounded by anything here.

Called by
3

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

What it touches

  • querycontent queryget_post()called directly
  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get_last_changed()one call below get_adjacent_post_rel_link()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
empty($post)19get_post(), get_adjacent_post()
empty($post)22–23get_post(), is_attachment(), get_adjacent_post()
is_attachment() && empty($post)23get_post(), is_attachment(), get_post()
!empty($post) && !empty($post_title)64–66get_post(), get_adjacent_post(), echo(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters()
!empty($post) && !empty($post_title)67–70get_post(), is_attachment(), get_adjacent_post(), echo(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters()
is_attachment() && !empty($post) && !empty($post_title)68–70get_post(), is_attachment(), get_post(), echo(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters()
!empty($post) && empty($post_title)70–73get_post(), get_adjacent_post(), echo(), __(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters()
!empty($post) && empty($post_title)73–77get_post(), is_attachment(), get_adjacent_post(), echo(), __(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters()
is_attachment() && !empty($post) && empty($post_title)74–77get_post(), is_attachment(), get_post(), echo(), __(), get_option(), mysql2date(), esc_attr(), get_permalink(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8919–7682 fewer instructions than PHP 8.5
8.59119–778
8.49119–7786 fewer instructions than PHP 8.3
8.39719–838
8.29719–838
8.19719–838
7.49719–838

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:

  1. apply_filters( {$adjacent}_post_rel_link )filterline 2102 (+49 into the body)

    Filters the adjacent post relational link.

Uses · 10

Used by · 3

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;	} 	$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.

  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.

7.0.4
Return type changed from string|void to string|null.verified against source
2.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.