wppaste
WordPress

block_core_breadcrumbs_get_terms_breadcrumbs( int $post_id, string $post_type ): array

Since
7.0.0
Source
wp-includes/blocks/breadcrumbs.php:500
Generates breadcrumb items from taxonomy terms.

Description

Finds the first publicly queryable taxonomy with terms assigned to the post and generates breadcrumb links, including hierarchical term ancestors if applicable.

Compatibility

WordPress
since 7.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_idint
The post ID.
$post_typestring
The post type name.

Return value

array
Array of breadcrumb item data.

Performance profile

How much work a call to block_core_breadcrumbs_get_terms_breadcrumbs() 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
Scales with input

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

Instructions
15–107

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

Plugin surface
1 hook

Third-party callbacks on 'block_core_breadcrumbs_post_type_settings' run inside this call, and their cost is not bounded by anything here.

Called by
1

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()one call below block_core_breadcrumbs_get_terms_breadcrumbs()
  • cacheobject cachewp_cache_add()one call below block_core_breadcrumbs_get_terms_breadcrumbs()

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 · 11 distinct outcomes

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

WhenInstructionsCalls it makes
empty($taxonomies)15get_object_taxonomies(), wp_filter_object_list()
!empty($taxonomies) && empty($terms)30–36get_object_taxonomies(), wp_filter_object_list(), apply_filters()
!empty($taxonomies) && !empty($settings) && !$taxonomies && empty($post_terms) && empty($terms)46–49get_object_taxonomies(), wp_filter_object_list(), apply_filters(), get_the_terms()
!empty($taxonomies) && empty($terms) && !$taxonomies && !empty($post_terms)49–55get_object_taxonomies(), wp_filter_object_list(), apply_filters(), get_the_terms(), is_wp_error()
!empty($taxonomies) && !empty($terms)55–72get_object_taxonomies(), wp_filter_object_list(), apply_filters(), reset(), block_core_breadcrumbs_get_term_ancestors_items(), array_merge(), get_term_link()
!empty($taxonomies) && !empty($settings) && !$taxonomies && empty($terms) && !is_wp_error()65get_object_taxonomies(), wp_filter_object_list(), apply_filters(), get_the_terms(), get_the_terms(), is_wp_error()
!empty($taxonomies) && !empty($settings) && !$taxonomies && !empty($post_terms) && empty($terms) && !is_wp_error()69–71get_object_taxonomies(), wp_filter_object_list(), apply_filters(), get_the_terms(), is_wp_error(), get_the_terms(), is_wp_error()
!empty($taxonomies) && !empty($settings) && !$taxonomies && empty($post_terms) && !empty($terms)71–85get_object_taxonomies(), wp_filter_object_list(), apply_filters(), get_the_terms(), reset(), block_core_breadcrumbs_get_term_ancestors_items(), array_merge(), get_term_link()
!empty($taxonomies) && !$taxonomies && !empty($post_terms) && !empty($terms)74–91get_object_taxonomies(), wp_filter_object_list(), apply_filters(), get_the_terms(), is_wp_error(), reset(), block_core_breadcrumbs_get_term_ancestors_items(), array_merge(), get_term_link()
!empty($taxonomies) && !empty($settings) && !$taxonomies && !is_wp_error()90–101get_object_taxonomies(), wp_filter_object_list(), apply_filters(), get_the_terms(), get_the_terms(), is_wp_error(), reset(), block_core_breadcrumbs_get_term_ancestors_items(), array_merge(), get_term_link()
!empty($taxonomies) && !empty($settings) && !$taxonomies && !empty($post_terms) && !is_wp_error()94–107get_object_taxonomies(), wp_filter_object_list(), apply_filters(), get_the_terms(), is_wp_error(), get_the_terms(), is_wp_error(), reset(), block_core_breadcrumbs_get_term_ancestors_items(), array_merge(), get_term_link()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 108 instructions, 15–107 executed per call, 18 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.

Hooks and filters fired · 1

One hook fires while block_core_breadcrumbs_get_terms_breadcrumbs() runs, in this order:

  1. apply_filters( block_core_breadcrumbs_post_type_settings )filterline 540 (+40 into the body)

    Filters breadcrumb settings (taxonomy and term selection) for a post or post type.

Uses · 7

Used by · 1

Source code

function block_core_breadcrumbs_get_terms_breadcrumbs( $post_id, $post_type ) {	$breadcrumb_items = array(); 	// Get public taxonomies for this post type.	$taxonomies = wp_filter_object_list(		get_object_taxonomies( $post_type, 'objects' ),		array(			'publicly_queryable' => true,			'show_in_rest'       => true,		)	); 	if ( empty( $taxonomies ) ) {		return $breadcrumb_items;	} 	/**	 * Filters breadcrumb settings (taxonomy and term selection) for a post or post type.	 *	 * Allows developers to specify which taxonomy and term should be used in the	 * breadcrumb trail when a post type has multiple taxonomies or when a post is	 * assigned to multiple terms within a taxonomy.	 *	 * @since 7.0.0	 *	 * @param array  $settings {	 *     Array of breadcrumb settings. Default empty array.	 *	 *     @type string $taxonomy Optional. Taxonomy slug to use for breadcrumbs.	 *                            The taxonomy must be registered for the post type and have	 *                            terms assigned to the post. If not found or has no terms,	 *                            fall back to the first available taxonomy with terms.	 *     @type string $term     Optional. Term slug to use when the post has multiple terms	 *                            in the selected taxonomy. If the term is not found or not	 *                            assigned to the post, fall back to the first term. If the	 *                            post has only one term, that term is used regardless.	 * }	 * @param string $post_type The post type slug.	 * @param int    $post_id   The post ID.	 */	$settings = apply_filters( 'block_core_breadcrumbs_post_type_settings', array(), $post_type, $post_id ); 	$taxonomy_name = null;	$terms         = array(); 	// Try preferred taxonomy first if specified.	if ( ! empty( $settings['taxonomy'] ) ) {		foreach ( $taxonomies as $taxonomy ) {			if ( $taxonomy->name === $settings['taxonomy'] ) {				$post_terms = get_the_terms( $post_id, $taxonomy->name );				if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {					$taxonomy_name = $taxonomy->name;					$terms         = $post_terms;				}				break;			}		}	} 	// If no preferred taxonomy or it didn't have terms, find the first taxonomy with terms.	if ( empty( $terms ) ) {		foreach ( $taxonomies as $taxonomy ) {			$post_terms = get_the_terms( $post_id, $taxonomy->name );			if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {				$taxonomy_name = $taxonomy->name;				$terms         = $post_terms;				break;			}		}	} 	if ( ! empty( $terms ) ) {		// Select which term to use.		$term = reset( $terms ); 		// Try preferred term if specified and post has multiple terms.		if ( ! empty( $settings['term'] ) && count( $terms ) > 1 ) {			foreach ( $terms as $candidate_term ) {				if ( $candidate_term->slug === $settings['term'] ) {					$term = $candidate_term;

Changelog

Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/blocks/breadcrumbs.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.