Title: Use block patterns in WP_Query php
Last modified: May 8, 2024

---

# Use block patterns in WP_Query php

 *  Resolved [rose18](https://wordpress.org/support/users/rose18/)
 * (@rose18)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/use-block-patterns-in-wp_query-php/)
 * Hi,
 * I was wondering if it’s possible to use block patterns inside php WP query loop?
   
   I’ve tried it (see code below), but the it’s returning nothing.
 *     ```wp-block-code
       <?php
       // WP_Query arguments
       $args = array(
       	'post_type'              => array( 'post' ),
       	'posts_per_page'         => -1,
       	'order'                  => 'DESC',
       	'orderby'                => 'date',
       );
   
       // The Query
       $query = new WP_Query( $args );
   
       // The Loop
       if ( $query->have_posts() ) {
       	while ( $query->have_posts() ) {
       		$query->the_post();
       		?>
       		<!-- wp:group {"layout":{"type":"default"}} -->
       		<div class="wp-block-group"><!-- wp:post-title /-->
   
       		<!-- wp:post-date {"style":{"elements":{"link":{"color":{"text":"var:preset|color|red"}}}},"textColor":"red","fontSize":"small"} /-->
   
       		<!-- wp:post-excerpt {"excerptLength":25} /--></div>
       		<!-- /wp:group -->
       		<?php
       /* PHP block pattern file */
       include 'loop-item-post.php';
   
       	}
       }
   
       // Restore original Post Data
       wp_reset_postdata();
   
       ?>
       ```
   
 * The core blocks (such as ‘title’, ‘date’ and ‘excerpt’) are not displaying anything
   on the front-end.
 * thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Joakim Navarro](https://wordpress.org/support/users/joa-kim/)
 * (@joa-kim)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/use-block-patterns-in-wp_query-php/#post-17744152)
 * I believe you can achieve this by using the function `render_block()` ([documentation](https://developer.wordpress.org/reference/functions/render_block/)).
   If you want to manually render blocks within PHP, you need to convert the block
   markup into rendered HTML.
 * Example expanding the code you provided:
 *     ```wp-block-code
       <?php
       // WP_Query arguments
       $args = array(
           'post_type'              => array('post'),
           'posts_per_page'         => -1,
           'order'                  => 'DESC',
           'orderby'                => 'date',
       );
   
       // The Query
       $query = new WP_Query($args);
   
       // The Loop
       if ($query->have_posts()) {
           while ($query->have_posts()) {
               $query->the_post();
   
               // Define your block pattern as a string
               $block_pattern = '<!-- wp:group -->
               <div class="wp-block-group"><!-- wp:post-title /-->
   
               <!-- wp:post-date {"style":{"elements":{"link":{"color":{"text":"var:preset|color|red"}}}},"textColor":"red","fontSize":"small"} /-->
   
               <!-- wp:post-excerpt {"excerptLength":25} /--></div>
               <!-- /wp:group -->';
   
               // Parse the block pattern
               $blocks = parse_blocks($block_pattern);
   
               // Render each block
               foreach ($blocks as $block) {
                   echo render_block($block);
               }
           }
           wp_reset_postdata();
       }
       ?>
       ```
   
 *  Thread Starter [rose18](https://wordpress.org/support/users/rose18/)
 * (@rose18)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/use-block-patterns-in-wp_query-php/#post-17744770)
 * WOW! Thank you [@joa-kim](https://wordpress.org/support/users/joa-kim/)! It works!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Use block patterns in WP_Query php’ is closed to new replies.

## Tags

 * [blocks](https://wordpress.org/support/topic-tag/blocks/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [rose18](https://wordpress.org/support/users/rose18/)
 * Last activity: [2 years, 1 month ago](https://wordpress.org/support/topic/use-block-patterns-in-wp_query-php/#post-17744770)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
