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.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
$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
- 382
- Plugin surface
- 1 hook
- Called by
- 0
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. The body compiles to 382.
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 - querycontent query
get_post()one call below render_block_core_latest_posts() - optionoption read or write
get_option()one call below render_block_core_latest_posts()
Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 382 | 382 | 49 | |
| 8.5 | 382 | 382 | 49 | |
| 8.4 | 382 | 382 | 49 | 14 fewer instructions than PHP 8.3 |
| 8.3 | 396 | 396 | 49 | |
| 8.2 | 396 | 396 | 49 | |
| 8.1 | 396 | 396 | 49 | |
| 7.4 | 396 | 396 | 49 |
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 167 (+122 into the body)
Filters the maximum number of words in a post excerpt.
Uses · 24
- 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 24
- 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_the_ID()Retrieves the ID of the current item in the WordPress Loop.
- get_post_field()Retrieves data from a post field based on Post ID.
- _wp_apply_block_content_filters()Applies standard content filters similar to the 'the_content' filter.
- setup_postdata()Set up global post data.
- remove_filter()Removes a callback function from a filter hook.
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- 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; static $rendering_stack = array(); $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( $args ); if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) { update_post_thumbnail_cache( $query ); } $list_items_markup = ''; // Snapshot the current global post so it can be restored after the loop. // We use have_posts()/the_post() rather than foreach so that setup_postdata() // is called for each post, establishing the correct global context for // do_blocks() and other filters that run during full-content rendering. $previous_post = $post ?? null; while ( $query->have_posts() ) { $query->the_post(); $post_link = esc_url( get_permalink() ); $title = get_the_title(); if ( ! $title ) { $title = __( '(no title)' ); } $list_items_markup .= '<li>'; if ( $attributes['displayFeaturedImage'] && has_post_thumbnail() ) { $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( null, $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_imageChangelog
Introduced in 5.0.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/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.