Skip to content

Instantly share code, notes, and snippets.

@infocities
infocities / _query-loop-patterns.md
Created April 22, 2025 07:56 — forked from joemaller/_query-loop-patterns.md
Behavior of pattern queries with the Query Loop block in the WordPress Block Editor.

Patterns are proving to be exceptionally useful, but there is some confusing, bug-like behavior with the core/query Query Loop block where patterns assigned to newly inserted blocks discard the pattern's query and fallback to the block's defaults. (as of March 2025).

  1. Insert a Query Loop block, click Choose, select pattern.
    Expectation: Use the specified pattern's query.
    Actual: The pattern specified in the query is ignored, query reverts to defaults (postType, perPage, order, etc.)

  2. Configure a Query Loop Block, then click Replace to choose a pattern
    Expectation: Either use the query in the pattern, or offer an option to replace the query?
    Actual: Existing query is preserved.
    This situation is not clear. Existing behavior makes some sense, since we've already configured the query. However there is still a query in the pattern which is being ignored.

/*** Custom template tag for styling an ACF date/time field (IMPORTANT, this works for the ACF Date/Time field only)
Set ACF date/time field return format to: Ymd H:i:s
Tag format: {{datetimeformat.event_start|M j, Y | g:i a}}
–––––––––––––––––––––––––––––––––––––––––––––––––– ***/
add_filter("render_block", "wsv_date_time_format_tags", 11, 2);
function wsv_date_time_format_tags($block_content, $block) {
// Updated pattern to match {{datetimeformat.<field>|<format>}} and allow the | character in the format
$pattern = "/{{datetimeformat\.([\w-]+)\|([,\w\s:-\|]+)}}/";
/*** Custom template tag for styling an ACF date field (IMPORTANT, this works for the ACF Date field only)
Set ACF date field return format to: Ymd
Tag format: {{dateformat.field_name|D M j}}
–––––––––––––––––––––––––––––––––––––––––––––––––– ***/
add_filter("render_block", "wsv_date_format_tags", 11, 2); // Updated function name
function wsv_date_format_tags($block_content, $block) {
// Updated pattern to allow commas, spaces, common date format characters, and the pipe (|) character in the format
$pattern = "/{{dateformat\.([\w-]+)\|([,\w\s\|-]+)}}/";
/*** Set default editor content for Video CPT using a specific Local Pattern
–––––––––––––––––––––––––––––––––––––––––––––––––– ***/
function add_or_replace_content_with_gb_template_for_video_posts($data, $postarr) {
// Only target the 'video' post type
if ($data['post_type'] == 'video') {
// Define your gblocks_templates Post ID
$gb_template_post_id = 5593; // Replace 5501 with your actual gblocks_templates Post ID
// Get the content of the gblocks_templates post
$gb_template_post = get_post($gb_template_post_id);
/*** Set default editor content for Video CPT using a specific Local Pattern
–––––––––––––––––––––––––––––––––––––––––––––––––– ***/
function add_or_replace_content_with_gb_template_for_video_posts($data, $postarr) {
// Only target the 'video' post type
if ($data['post_type'] == 'cpt_name') {
// Define your gblocks_templates Post ID
$gb_template_post_id = 123; // Replace 5501 with your actual gblocks_templates Post ID
// Get the content of the gblocks_templates post
$gb_template_post = get_post($gb_template_post_id);
/*** Set ACF field default as Post ID for the modal_id field for the video and match CPTs
–––––––––––––––––––––––––––––––––––––––––––––––––– ***/
function wsv_post_id_default_for_modal_id($post_id) {
// Check if we're saving a 'video' or 'match' post type
$post_type = get_post_type($post_id);
if ($post_type == 'video' || $post_type == 'match') {
// Retrieve the current value of the 'modal_id' field
$current_value = get_field('modal_id', $post_id);
// If the 'modal_id' field is empty, set it to the post ID
<?php
$gblock = get_post(1234);
echo apply_filters('the_content', $gblock->post_content);
?>
add_action('acf/init', 'my_acf_init_blocks');
function my_acf_init_blocks() {
// Check function exists.
if( function_exists('acf_register_block_type') ) {
// Register a restricted block.
acf_register_block_type(array(
'name' => 'restricted',
@infocities
infocities / inner-example.php
Created April 22, 2025 07:52 — forked from ptesei/inner-example.php
inner-example.php
<?php
/**
* Restricted Block Template.
*
* @param array $block The block settings and attributes.
* @param string $content The block inner HTML (empty).
* @param bool $is_preview True during AJAX preview.
* @param (int|string) $post_id The post ID this block is saved to.
*/