render_block_core_latest_posts( array $attributes ): string
- Since
- 5.0.0
- Source
wp-includes/blocks/latest-posts.php:45
core/latest-posts block on server.Compatibility
- WordPress
- since 5.0.0
- PHP
- 7.4–8.6-dev
- 6.7.10
- 6.8.11
- 6.9.10
- 7.0.7
- 7.1.3
Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$attributesarray- The block attributes.
Return value
string- Returns the post content with latest posts added.
Performance profile
How much work a call to render_block_core_latest_posts() 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
- 68–90
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via ->query().
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 334.
Third-party callbacks on 'excerpt_length' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - sqldatabase query
->query()called directly - querycontent query
get_post()one call below render_block_core_latest_posts() - cacheobject cache
wp_cache_get()one call below render_block_core_latest_posts() - serializeserialisation
maybe_unserialize()one call below render_block_core_latest_posts()
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 · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost render_block_core_latest_posts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($attributes) && !isset($attributes) | 68–90 | add_filter(), posts_per_page(), ->query(), remove_filter(), class(), sprintf() |
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 | 334 | 68–90 | 39 | |
| 8.5 | 334 | 68–90 | 39 | |
| 8.4 | 334 | 68–90 | 39 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 342 | 71–93 | 39 | |
| 8.2 | 342 | 71–93 | 39 | |
| 8.1 | 342 | 71–93 | 39 | |
| 7.4 | 342 | 71–93 | 39 |
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 render_block_core_latest_posts() runs, in this order:
- apply_filters( excerpt_length )filterline 161 (+116 into the body)
Filters the maximum number of words in a post excerpt.
Uses · 21
- add_filter()Adds a callback function to a filter hook.
- update_post_thumbnail_cache()Updates cache for thumbnails in the current loop.
- esc_url()Checks and cleans a URL.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- get_the_title()Retrieves the post title.
- __()Retrieves the translation of $text.
- has_post_thumbnail()Determines whether a post has an image attached.
- get_the_post_thumbnail()Retrieves the post thumbnail.
- esc_attr()Escaping for HTML attributes.
- get_the_author_meta()Retrieves the requested data of the author of the current post.
- get_the_date()Retrieves the date of the post.
- get_the_excerpt()Retrieves the post excerpt.
Show all 21
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_html()Escaping for HTML blocks.
- post_password_required()Determines whether the post requires password and whether a correct password has been provided.
- get_option()Retrieves an option value based on an option name.
- wp_kses_post()Sanitizes content for allowed HTML tags for post content.
- remove_filter()Removes a callback function from a filter hook.
- get_block_wrapper_attributes()Generates a string of attributes by applying to the current block being rendered all of the features that the block supports.
- WP_Query::__construct()Constructor.
Source code
function render_block_core_latest_posts( $attributes ) { global $post, $block_core_latest_posts_excerpt_length; $args = array( 'posts_per_page' => $attributes['postsToShow'], 'post_status' => 'publish', 'order' => $attributes['order'], 'orderby' => $attributes['orderBy'], 'ignore_sticky_posts' => true, 'no_found_rows' => true, ); $block_core_latest_posts_excerpt_length = $attributes['excerptLength']; add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 ); if ( ! empty( $attributes['categories'] ) ) { $args['category__in'] = array_column( $attributes['categories'], 'id' ); } if ( isset( $attributes['selectedAuthor'] ) ) { $args['author'] = $attributes['selectedAuthor']; } $query = new WP_Query(); $recent_posts = $query->query( $args ); if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) { update_post_thumbnail_cache( $query ); } $list_items_markup = ''; foreach ( $recent_posts as $post ) { $post_link = esc_url( get_permalink( $post ) ); $title = get_the_title( $post ); if ( ! $title ) { $title = __( '(no title)' ); } $list_items_markup .= '<li>'; if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) { $image_style = ''; if ( isset( $attributes['featuredImageSizeWidth'] ) ) { $image_style .= sprintf( 'max-width:%spx;', $attributes['featuredImageSizeWidth'] ); } if ( isset( $attributes['featuredImageSizeHeight'] ) ) { $image_style .= sprintf( 'max-height:%spx;', $attributes['featuredImageSizeHeight'] ); } $image_classes = 'wp-block-latest-posts__featured-image'; if ( isset( $attributes['featuredImageAlign'] ) ) { $image_classes .= ' align' . $attributes['featuredImageAlign']; } $featured_image = get_the_post_thumbnail( $post, $attributes['featuredImageSizeSlug'], array( 'style' => esc_attr( $image_style ), ) ); if ( $attributes['addLinkToFeaturedImage'] ) { $featured_image = sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', esc_url( $post_link ), esc_attr( $title ), $featured_image ); } $list_items_markup .= sprintf( '<div class="%1$s">%2$s</div>', esc_attr( $image_classes ), $featured_image ); } $list_items_markup .= sprintf( '<a class="wp-block-latest-posts__post-title" href="%1$s">%2$s</a>', esc_url( $post_link ),Changelog
Introduced in 5.0.0. Unchanged from 6.7.10 through 7.1.3.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.7 tag, from
src/wp-includes/blocks/latest-posts.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.