wppaste
WordPress

get_adjacent_post( bool $in_same_term = false, int[]|string $excluded_terms = '', bool $previous = true, string $taxonomy = 'category' ): WP_Post|null|string

Since
2.5.0
Source
wp-includes/link-template.php:1829
Retrieves the adjacent post.

Description

Can either be next or previous post.

Compatibility

WordPress
since 2.5.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

$in_same_termbooloptional
Whether post 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 string.Default: ''
$previousbooloptional
Whether to retrieve previous post.
Default true.Default: true
$taxonomystringoptional
Taxonomy, if $in_same_term is true. Default 'category'.Default: 'category'

Return value

WP_Post|null|string
Post object if successful. Null if global $post is not set.
Empty string if no corresponding post exists.

Performance profile

How much work a call to get_adjacent_post() 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_var().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
10–244

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

Plugin surface
4 hooks

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

Called by
7

7 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
  • cacheobject cachewp_cache_get_last_changed()called directly
  • sqldatabase query->get_var()called directly

Further down the call graph this can also reach option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 68 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_adjacent_post() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always10get_post()
taxonomy_exists() && !is_object_in_taxonomy()51–54get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy()
taxonomy_exists() && is_object_in_taxonomy() && is_wp_error()63–66get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error()70–73get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff()
taxonomy_exists() && empty($excluded_terms) && !is_user_logged_in() && $result !== false130–137get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && empty($excluded_terms) && !is_user_logged_in() && $result !== false133–142get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && empty($excluded_terms) && !is_user_logged_in() && $result !== false134–141get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && !is_user_logged_in() && $result !== false137–146get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && !is_user_logged_in() && $result === false142–150get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && empty($excluded_terms) && !is_user_logged_in() && $result === false145–155get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && empty($excluded_terms) && !is_user_logged_in() && $result === false146–154get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && !is_user_logged_in() && $result !== false148–151get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted()
56 further outcomes, up to 244 instructions
taxonomy_exists() && empty($excluded_terms) && !is_user_logged_in() && $result === false149–159get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && !empty($excluded_terms) && !is_user_logged_in() && $result !== false151–155get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && empty($excluded_terms) && is_user_logged_in() && $result !== false151–161get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && !is_user_logged_in() && $result !== false152–155get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_user_logged_in() && $result !== false154–166get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && !empty($excluded_terms) && !is_user_logged_in() && $result !== false155–159get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_user_logged_in() && $result !== false155–165get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_user_logged_in() && $result !== false158–170get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && !is_user_logged_in() && $result === false160–164get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && !empty($excluded_terms) && !is_user_logged_in() && $result === false163–168get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && empty($excluded_terms) && is_user_logged_in() && $result === false163–174get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && !is_user_logged_in() && $result === false164–168get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_user_logged_in() && $result === false166–179get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && !empty($excluded_terms) && !is_user_logged_in() && $result === false167–172get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_user_logged_in() && $result === false167–178get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && is_user_logged_in() && $result !== false169–174get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && empty($excluded_terms) && is_user_logged_in() && $result === false170–183get_post(), taxonomy_exists(), apply_filters(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && is_user_logged_in() && $result !== false172–179get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && is_user_logged_in() && $result !== false173–178get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && is_user_logged_in() && $result !== false176–183get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result !== false178–183get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && is_user_logged_in() && $result === false181–187get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result !== false181–188get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result !== false182–187get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && is_user_logged_in() && $result === false184–192get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && is_user_logged_in() && $result === false185–191get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result !== false185–192get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && is_user_logged_in() && $result === false188–196get_post(), taxonomy_exists(), apply_filters(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result === false190–196get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result !== false193–198get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result === false193–201get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result === false194–200get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !empty($excluded_terms) && !is_user_logged_in() && $result !== false196–203get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result !== false197–202get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result === false197–205get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result !== false199–207get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !empty($excluded_terms) && !is_user_logged_in() && $result !== false200–207get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result !== false202–212get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result !== false203–211get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result === false205–211get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result !== false206–216get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !empty($excluded_terms) && !is_user_logged_in() && $result === false208–216get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !is_user_logged_in() && $result === false209–215get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result === false211–220get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !empty($excluded_terms) && !is_user_logged_in() && $result === false212–220get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result !== false214–222get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result === false214–225get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result === false215–224get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !empty($excluded_terms) && is_user_logged_in() && $result !== false217–227get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result !== false218–226get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && empty($excluded_terms) && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result === false218–229get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !empty($excluded_terms) && is_user_logged_in() && $result !== false221–231get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), get_post()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result === false226–235get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !empty($excluded_terms) && is_user_logged_in() && $result === false229–240get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && is_user_logged_in() && $result === false230–239get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()
taxonomy_exists() && is_object_in_taxonomy() && !is_wp_error() && !empty($excluded_terms) && is_user_logged_in() && $result === false233–244get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy(), wp_get_object_terms(), is_wp_error(), array_diff(), array_map(), array_map(), is_user_logged_in(), get_current_user_id(), get_post_type_object(), get_post_stati(), apply_filters(), ->prepare(), apply_filters(), apply_filters(), md5(), wp_cache_get_last_changed(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_var(), wp_cache_set_salted(), get_post()

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

PHPCompiledExecutedBranchesNotes
8.6-dev31010–24426
8.531010–24426
8.431010–244269 fewer instructions than PHP 8.3
8.331910–25026
8.231910–25026
8.131910–25026
7.431910–25026

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 · 4

4 hooks fire while get_adjacent_post() runs, in this order:

  1. apply_filters( get_{$adjacent}_post_excluded_terms )filterline 1879 (+50 into the body)

    Filters the IDs of terms excluded from adjacent post queries.

  2. apply_filters( get_{$adjacent}_post_join )filterline 1964 (+135 into the body)

    Filters the JOIN clause in the SQL for an adjacent post query.

  3. apply_filters( get_{$adjacent}_post_where )filterline 1990 (+161 into the body)

    Filters the WHERE clause in the SQL for an adjacent post query.

  4. apply_filters( get_{$adjacent}_post_sort )filterline 2012 (+183 into the body)

    Filters the ORDER BY clause in the SQL for an adjacent post query.

Uses · 17

Show all 17

Used by · 7

Source code

function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {	global $wpdb; 	$post = get_post(); 	if ( ! $post || ! taxonomy_exists( $taxonomy ) ) {		return null;	} 	$current_post_date = $post->post_date; 	$join     = '';	$where    = '';	$adjacent = $previous ? 'previous' : 'next'; 	if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {		// Back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and ".		if ( str_contains( $excluded_terms, ' and ' ) ) {			_deprecated_argument(				__FUNCTION__,				'3.3.0',				sprintf(					/* translators: %s: The word 'and'. */					__( 'Use commas instead of %s to separate excluded terms.' ),					"'and'"				)			);			$excluded_terms = explode( ' and ', $excluded_terms );		} else {			$excluded_terms = explode( ',', $excluded_terms );		} 		$excluded_terms = array_map( 'intval', $excluded_terms );	} 	/**	 * Filters the IDs of terms excluded from adjacent post queries.	 *	 * The dynamic portion of the hook name, `$adjacent`, refers to the type	 * of adjacency, 'next' or 'previous'.	 *	 * Possible hook names include:	 *	 *  - `get_next_post_excluded_terms`	 *  - `get_previous_post_excluded_terms`	 *	 * @since 4.4.0	 *	 * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.	 */	$excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms ); 	if ( $in_same_term || ! empty( $excluded_terms ) ) {		if ( $in_same_term ) {			$join  .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";			$where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy ); 			if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {				return '';			}			$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );			if ( is_wp_error( $term_array ) ) {				return '';			} 			// Remove any exclusions from the term array to include.			$term_array = array_diff( $term_array, (array) $excluded_terms ); 			if ( ! $term_array ) {				return '';			} 			$term_array = array_map( 'intval', $term_array ); 			$where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')';		} 		if ( ! empty( $excluded_terms ) ) {			$where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ',', array_map( 'intval', $excluded_terms ) ) . ') )';		}

Changelog

Introduced in 2.5.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 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.