wppaste
WordPress

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:2345
Retrieves the adjacent post link.

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.
$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_term is 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

Reaches the database via get_post().

Scaling
Constant

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

Instructions
30–83

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

Plugin surface
2 hooks

Third-party callbacks on 'the_title', '{$adjacent}_post_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
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • cacheobject cachewp_cache_get_last_changed()one call below get_adjacent_post_link()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
always30–31get_adjacent_post(), apply_filters()
!is_attachment()33–34is_attachment(), get_adjacent_post(), apply_filters()
is_attachment()35–36is_attachment(), get_post(), get_post(), apply_filters()
!empty($post)69–71get_adjacent_post(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters()
!is_attachment() && !empty($post)72–74is_attachment(), get_adjacent_post(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters()
is_attachment() && !empty($post)74–76is_attachment(), get_post(), get_post(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters()
empty($post)75–78get_adjacent_post(), __(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters()
!is_attachment() && empty($post)78–81is_attachment(), get_adjacent_post(), __(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters()
is_attachment() && empty($post)80–83is_attachment(), get_post(), get_post(), __(), apply_filters(), get_option(), mysql2date(), get_permalink(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9630–8272 fewer instructions than PHP 8.5
8.59830–837
8.49830–83711 fewer instructions than PHP 8.3
8.310930–947
8.210930–947
8.110930–9471 fewer instruction than PHP 7.4
7.411030–957

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:

  1. apply_filters( the_title )filterline 2362 (+17 into the body)

    Filters the post title.

  2. apply_filters( {$adjacent}_post_link )filterline 2397 (+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

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.

  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.

About this page

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