Conversation
supportActually, when you apply the filter query_loop_block_query_vars on pre_render_block, all the query after your block will be affected. You should instead use the hook outside pre_render_block, and fetch your conditions inside this hook : add_filter( 'query_loop_block_query_vars', __NAMESPACE__ . '\filter_my_query', 10, 2 ); function filter_my_query( $query, $block ) { if ( isset( $block->parsed_block['attrs']['namespace'] ) && 'creativeandrew/single-post-query-loop-selector' === $block->parsed_block['attrs']['namespace'] ) { if ( isset( $block->parsed_block['attrs']['query']['include'] ) ) { $query['post__in'] = $block->parsed_block['attrs']['query']['include']; } } return $query; } Have a nice day ! This topic was modified 2 years, 2 months ago by Willy Bahuaud . This topic was modified 2 years, 2 months ago by Willy Bahuaud .
Sorry, wrong code… updated here: add_filter( 'query_loop_block_query_vars', __NAMESPACE__ . '\filter_my_query', 10, 2 ); function filter_my_query( $query, $block ) { if ( ! empty( $block->context['query']['include'] ) ) { $query['post__in'] = $block->context['query']['include']; } return $query; }
Hi Willy , Thanks a lot for your message. Yes, this was reported here as well: https://github.com/creative-andrew/single-post-query-loop-selector/issues/1 I will be amending the code shortly and submitting a new release!
However, wouldn’t it be better to do: add_filter( 'query_loop_block_query_vars', __NAMESPACE__ . '\filter_my_query', 10, 2 ); function filter_my_query( $query, $block ) { if ( isset( $block->parsed_block['attrs']['namespace'] ) && 'creativeandrew/single-post-query-loop-selector' === $block->parsed_block['attrs']['namespace'] && ! empty( $block->context['query']['include'] ) ) { $query['post__in'] = $block->context['query']['include']; } return $query; } This reply was modified 2 years, 2 months ago by Creative Andrew .
While working on this I noticed a couple of things. The query_loop_block_query_vars filter’s $block param doesn’t pass the variation itself. So it’s not possible to check 'creativeandrew/single-post-query-loop-selector' === $block->parsed_block['attrs']['namespace'] That might explain why in the documentation it is mentioned the need to have access to the pre_render_block . I was able to avoid my filter from being applied to other queries by comparing the queryId from the one coming from the pre_render_block and query_loop_block_query_vars function pre_render_block( $pre_render, $parsed_block ) { if ( isset( $parsed_block['attrs']['namespace'] ) && 'creativeandrew/single-post-query-loop-selector' === $parsed_block['attrs']['namespace'] ) { add_filter( 'query_loop_block_query_vars', function ( $default_query, $block ) use ( $parsed_block ) { if ( isset( $parsed_block['attrs']['query']['include'] ) && isset( $block->context['queryId'] ) && $block->context['queryId'] === $parsed_block['attrs']['queryId'] ) { $default_query['post__in'] = $parsed_block['attrs']['query']['include']; } return $default_query; }, 10, 2 ); } return $pre_render; } This reply was modified 2 years, 2 months ago by Creative Andrew . This reply was modified 2 years, 2 months ago by Creative Andrew . This reply was modified 2 years, 2 months ago by Creative Andrew .
Problem fixed in latest version
Sorry, wrong code… updated here: add_filter( 'query_loop_block_query_vars', __NAMESPACE__ . '\filter_my_query', 10, 2 ); function filter_my_query( $query, $block ) { if ( ! empty( $block->context['query']['include'] ) ) { $query['post__in'] = $block->context['query']['include']; } return $query; }
Hi Willy , Thanks a lot for your message. Yes, this was reported here as well: https://github.com/creative-andrew/single-post-query-loop-selector/issues/1 I will be amending the code shortly and submitting a new release!
However, wouldn’t it be better to do: add_filter( 'query_loop_block_query_vars', __NAMESPACE__ . '\filter_my_query', 10, 2 ); function filter_my_query( $query, $block ) { if ( isset( $block->parsed_block['attrs']['namespace'] ) && 'creativeandrew/single-post-query-loop-selector' === $block->parsed_block['attrs']['namespace'] && ! empty( $block->context['query']['include'] ) ) { $query['post__in'] = $block->context['query']['include']; } return $query; } This reply was modified 2 years, 2 months ago by Creative Andrew .
While working on this I noticed a couple of things. The query_loop_block_query_vars filter’s $block param doesn’t pass the variation itself. So it’s not possible to check 'creativeandrew/single-post-query-loop-selector' === $block->parsed_block['attrs']['namespace'] That might explain why in the documentation it is mentioned the need to have access to the pre_render_block . I was able to avoid my filter from being applied to other queries by comparing the queryId from the one coming from the pre_render_block and query_loop_block_query_vars function pre_render_block( $pre_render, $parsed_block ) { if ( isset( $parsed_block['attrs']['namespace'] ) && 'creativeandrew/single-post-query-loop-selector' === $parsed_block['attrs']['namespace'] ) { add_filter( 'query_loop_block_query_vars', function ( $default_query, $block ) use ( $parsed_block ) { if ( isset( $parsed_block['attrs']['query']['include'] ) && isset( $block->context['queryId'] ) && $block->context['queryId'] === $parsed_block['attrs']['queryId'] ) { $default_query['post__in'] = $parsed_block['attrs']['query']['include']; } return $default_query; }, 10, 2 ); } return $pre_render; } This reply was modified 2 years, 2 months ago by Creative Andrew . This reply was modified 2 years, 2 months ago by Creative Andrew . This reply was modified 2 years, 2 months ago by Creative Andrew .
Problem fixed in latest version