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
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_termis true. Default 'category'.Default:'category'
Return value
WP_Post|null|string- Post object if successful. Null if global
$postis 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
- Scaling
- Scales with input
- Instructions
- 10–244
- Plugin surface
- 4 hooks
- Called by
- 7
Reaches the database via ->get_var().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 310.
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.
7 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 - cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10 | get_post() |
taxonomy_exists() && !is_object_in_taxonomy() | 51–54 | get_post(), taxonomy_exists(), apply_filters(), ->prepare(), is_object_in_taxonomy() |
taxonomy_exists() && is_object_in_taxonomy() && is_wp_error() | 63–66 | get_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–73 | get_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 !== false | 130–137 | get_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 !== false | 133–142 | get_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 !== false | 134–141 | get_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 !== false | 137–146 | get_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 === false | 142–150 | get_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 === false | 145–155 | get_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 === false | 146–154 | get_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 !== false | 148–151 | get_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 === false | 149–159 | get_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 !== false | 151–155 | get_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 !== false | 151–161 | get_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 !== false | 152–155 | get_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 !== false | 154–166 | get_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 !== false | 155–159 | get_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 !== false | 155–165 | get_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 !== false | 158–170 | get_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 === false | 160–164 | get_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 === false | 163–168 | get_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 === false | 163–174 | get_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 === false | 164–168 | get_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 === false | 166–179 | get_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 === false | 167–172 | get_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 === false | 167–178 | get_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 !== false | 169–174 | get_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 === false | 170–183 | get_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 !== false | 172–179 | get_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 !== false | 173–178 | get_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 !== false | 176–183 | get_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 !== false | 178–183 | get_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 === false | 181–187 | get_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 !== false | 181–188 | get_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 !== false | 182–187 | get_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 === false | 184–192 | get_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 === false | 185–191 | get_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 !== false | 185–192 | get_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 === false | 188–196 | get_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 === false | 190–196 | get_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 !== false | 193–198 | get_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 === false | 193–201 | get_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 === false | 194–200 | get_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 !== false | 196–203 | get_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 !== false | 197–202 | get_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 === false | 197–205 | get_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 !== false | 199–207 | get_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 !== false | 200–207 | get_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 !== false | 202–212 | get_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 !== false | 203–211 | get_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 === false | 205–211 | get_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 !== false | 206–216 | get_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 === false | 208–216 | get_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 === false | 209–215 | get_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 === false | 211–220 | get_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 === false | 212–220 | get_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 !== false | 214–222 | get_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 === false | 214–225 | get_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 === false | 215–224 | get_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 !== false | 217–227 | get_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 !== false | 218–226 | get_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 === false | 218–229 | get_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 !== false | 221–231 | get_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 === false | 226–235 | get_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 === false | 229–240 | get_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 === false | 230–239 | get_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 === false | 233–244 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 310 | 10–244 | 26 | |
| 8.5 | 310 | 10–244 | 26 | |
| 8.4 | 310 | 10–244 | 26 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 319 | 10–250 | 26 | |
| 8.2 | 319 | 10–250 | 26 | |
| 8.1 | 319 | 10–250 | 26 | |
| 7.4 | 319 | 10–250 | 26 |
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:
- apply_filters( get_{$adjacent}_post_excluded_terms )filterline 1879 (+50 into the body)
Filters the IDs of terms excluded from adjacent post queries.
- apply_filters( get_{$adjacent}_post_join )filterline 1964 (+135 into the body)
Filters the JOIN clause in the SQL for an adjacent post query.
- apply_filters( get_{$adjacent}_post_where )filterline 1990 (+161 into the body)
Filters the WHERE clause in the SQL for an adjacent post query.
- 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
- get_post()Retrieves post data given a post ID or post object.
- taxonomy_exists()Determines whether the taxonomy name exists.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_object_in_taxonomy()Determines if the given object type is associated with the given taxonomy.
- wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- is_user_logged_in()Determines whether the current visitor is a logged in user.
- get_current_user_id()Gets the current user's ID.
- get_post_type_object()Retrieves a post type object by name.
Show all 17
- get_post_stati()Gets a list of post statuses.
- current_user_can()Returns whether the current user has the specified capability.
- wp_cache_get_last_changed()Gets last changed date for the specified cache group.
- wp_cache_get_salted()Retrieves cached data if valid and unchanged.
- wp_cache_set_salted()Stores salted data in the cache.
Used by · 7
- get_adjacent_post_link()Retrieves the adjacent post link.
- get_adjacent_post_rel_link()Retrieves the adjacent post relational link.
- get_next_post()Retrieves the next post that is adjacent to the current post.
- get_previous_post()Retrieves the previous post that is adjacent to the current post.
- twentyfifteen_post_nav_background()Adds featured image as background image to post navigation elements.
- twentyfourteen_post_nav()Displays navigation to next/previous post when applicable.
- twentythirteen_post_nav()Displays navigation to next/previous post when applicable.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.