get_boundary_post( bool $in_same_term = false, int[]|string $excluded_terms = '', bool $start = true, string $taxonomy = 'category' ): array|null
- Since
- 2.8.0
- Source
wp-includes/link-template.php:2201
Description
Boundary being either the first or last post by publish date within the constraints specified by $in_same_term or $excluded_terms.
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
$in_same_termbooloptional- Whether returned 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.Default:'' $startbooloptional- Whether to retrieve first or last post.
Default true.Default:true $taxonomystringoptional- Taxonomy, if
$in_same_termis true. Default 'category'.Default:'category'
Return value
array|null- Array containing the boundary post object if successful, null otherwise.
Performance profile
How much work a call to get_boundary_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
- 9–79
- Plugin surface
- None
- Called by
- 1
Reaches the database via get_post().
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 86.
Nothing here hands control to plugin code.
1 place 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()one call below get_boundary_post()
Further down the call graph this can also reach option, cache, 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 · 14 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_boundary_post() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9 | get_post() |
!is_single() | 12 | get_post(), is_single() |
is_single() && is_attachment() | 15 | get_post(), is_single(), is_attachment() |
is_single() && !is_attachment() && !taxonomy_exists() | 19 | get_post(), is_single(), is_attachment(), taxonomy_exists() |
is_single() && !is_attachment() && taxonomy_exists() && empty($excluded_terms) | 35–39 | get_post(), is_single(), is_attachment(), taxonomy_exists(), get_posts() |
is_single() && !is_attachment() && taxonomy_exists() && !is_array($excluded_terms) | 43–44 | get_post(), is_single(), is_attachment(), taxonomy_exists(), explode(), get_posts() |
is_single() && !is_attachment() && taxonomy_exists() | 47–51 | get_post(), is_single(), is_attachment(), taxonomy_exists(), array_merge(), get_posts() |
is_single() && !is_attachment() && taxonomy_exists() && empty($excluded_terms) | 52–59 | get_post(), is_single(), is_attachment(), taxonomy_exists(), wp_get_object_terms(), array_merge(), get_posts() |
is_single() && !is_attachment() && taxonomy_exists() && !is_array($excluded_terms) | 55–56 | get_post(), is_single(), is_attachment(), taxonomy_exists(), explode(), array_merge(), get_posts() |
is_single() && !is_attachment() && taxonomy_exists() && !is_array($excluded_terms) | 60–64 | get_post(), is_single(), is_attachment(), taxonomy_exists(), explode(), wp_get_object_terms(), array_merge(), get_posts() |
is_single() && !is_attachment() && taxonomy_exists() && !empty($excluded_terms) | 61–66 | get_post(), is_single(), is_attachment(), taxonomy_exists(), array_map(), array_diff(), array_merge(), get_posts() |
is_single() && !is_attachment() && taxonomy_exists() && !empty($excluded_terms) | 66–74 | get_post(), is_single(), is_attachment(), taxonomy_exists(), wp_get_object_terms(), array_map(), array_diff(), array_merge(), get_posts() |
2 further outcomes, up to 79 instructions
is_single() && !is_attachment() && taxonomy_exists() && !is_array($excluded_terms) && !empty($excluded_terms) | 69–71 | get_post(), is_single(), is_attachment(), taxonomy_exists(), explode(), array_map(), array_diff(), array_merge(), get_posts() |
is_single() && !is_attachment() && taxonomy_exists() && !is_array($excluded_terms) && !empty($excluded_terms) | 74–79 | get_post(), is_single(), is_attachment(), taxonomy_exists(), explode(), wp_get_object_terms(), array_map(), array_diff(), array_merge(), get_posts() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 86 instructions, 9–79 executed per call, 13 branches. The work does not change between versions.
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.
Uses · 6
- get_post()Retrieves post data given a post ID or post object.
- is_single()Determines whether the query is for an existing single post.
- is_attachment()Determines whether the query is for an existing attachment page.
- taxonomy_exists()Determines whether the taxonomy name exists.
- wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
- get_posts()Retrieves an array of the latest posts, or posts matching the given criteria.
Used by · 1
- get_boundary_post_rel_link()Get boundary post relational link.
Source code
function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { $post = get_post(); if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) { return null; } $query_args = array( 'posts_per_page' => 1, 'order' => $start ? 'ASC' : 'DESC', 'update_post_term_cache' => false, 'update_post_meta_cache' => false, ); $term_array = array(); if ( ! is_array( $excluded_terms ) ) { if ( ! empty( $excluded_terms ) ) { $excluded_terms = explode( ',', $excluded_terms ); } else { $excluded_terms = array(); } } if ( $in_same_term || ! empty( $excluded_terms ) ) { if ( $in_same_term ) { $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); } if ( ! empty( $excluded_terms ) ) { $excluded_terms = array_map( 'intval', $excluded_terms ); $excluded_terms = array_diff( $excluded_terms, $term_array ); $inverse_terms = array(); foreach ( $excluded_terms as $excluded_term ) { $inverse_terms[] = $excluded_term * -1; } $excluded_terms = $inverse_terms; } $query_args['tax_query'] = array( array( 'taxonomy' => $taxonomy, 'terms' => array_merge( $term_array, $excluded_terms ), ), ); } return get_posts( $query_args );}Changelog
Introduced in 2.8.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.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.