wppaste
WordPress

render_block_core_latest_posts( array $attributes ): string

Since
5.0.0
Source
wp-includes/blocks/latest-posts.php:45
Renders the 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

Reaches the database via get_post().

Scaling
Scales with input

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

Instructions
382

Executed per call on PHP 8.5. The body compiles to 382.

Plugin surface
1 hook

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

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()one call below render_block_core_latest_posts()
  • optionoption read or writeget_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

PHPCompiledExecutedBranchesNotes
8.6-dev38238249
8.538238249
8.43823824914 fewer instructions than PHP 8.3
8.339639649
8.239639649
8.139639649
7.439639649

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:

  1. apply_filters( excerpt_length )filterline 167 (+122 into the body)

    Filters the maximum number of words in a post excerpt.

Uses · 24

Show all 24

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_image

Changelog

Introduced in 5.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.