# gutenberg/8.9.2/build/block-library/blocks/query-loop.php

Gutenberg, version 8.9.2. 59 lines.

- Page: https://pluginprobe.com/plugins/gutenberg/8.9.2/code/build/block-library/blocks/query-loop.php
- Raw: https://pluginprobe.com/plugins/gutenberg/8.9.2/raw/build/block-library/blocks/query-loop.php
- Modified: 2020-05-27T16:27:08+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/gutenberg/8.9.2/code/build/block-library/blocks/query-loop.php#L10-L20`.

```php
<?php
/**
 * Server-side rendering of the `core/query-loop` block.
 *
 * @package WordPress
 */

/**
 * Renders the `core/query-loop` block on the server.
 *
 * @param array    $attributes Block attributes.
 * @param string   $content    Block default content.
 * @param WP_Block $block      Block instance.
 *
 * @return string Returns the output of the query, structured using the layout defined by the block's inner blocks.
 */
function gutenberg_render_block_core_query_loop( $attributes, $content, $block ) {
	$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
	$page     = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT );

	$query = array(
		'post_type'    => 'post',
		'offset'       => isset( $block->context['query']['perPage'] ) ? ( $block->context['query']['perPage'] * ( $page - 1 ) + $block->context['query']['offset'] ) : 0,
		'category__in' => $block->context['query']['categoryIds'],
	);
	if ( isset( $block->context['query']['perPage'] ) ) {
		$query['posts_per_page'] = $block->context['query']['perPage'];
	}
	$posts = get_posts( $query );

	$content = '';
	foreach ( $posts as $post ) {
		$content .= (
			new WP_Block(
				$block->parsed_block,
				array(
					'postType' => $post->post_type,
					'postId'   => $post->ID,
				)
			)
		)->render( array( 'dynamic' => false ) );
	}
	return $content;
}

/**
 * Registers the `core/query-loop` block on the server.
 */
function gutenberg_register_block_core_query_loop() {
	register_block_type_from_metadata(
		__DIR__ . '/query-loop',
		array(
			'render_callback'   => 'gutenberg_render_block_core_query_loop',
			'skip_inner_blocks' => true,
		)
	);
}
add_action( 'init', 'gutenberg_register_block_core_query_loop', 20 );

```
