• Resolved Willy Bahuaud

    (@willybahuaud)


    Actually, 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 !

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Willy Bahuaud

    (@willybahuaud)

    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;
    }
    Plugin Author Creative Andrew

    (@geisthanen)

    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!

    Plugin Author Creative Andrew

    (@geisthanen)

    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;
    }
    Plugin Author Creative Andrew

    (@geisthanen)

    While working on this I noticed a couple of things.

    1. 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']
    2. That might explain why in the documentation it is mentioned the need to have access to the pre_render_block.
    3. 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;
    }
    
    Plugin Author Creative Andrew

    (@geisthanen)

    Problem fixed in latest version

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘You should replace your query’ is closed to new replies.