Search code examples
phpwordpressshortcodewordpress-gutenberggutenberg-blocks

How to reuse page queries in custom gutenberg blocks or shortcodes?


I'd like to solve this problem, but not sure if it's even possible...

Say I've got a page which on header shows some terms, so I start retrieving them:

  $children_terms = get_terms(array(
    'taxonomy' => $taxonomy_name,
    'hide_empty' => false,
    'parent' => $related_product_term->term_id
  ));

Great. Then I start post_content output, which is plain gutenberg blocks.

In the middle of the page contents from gutenberg, I would need to show again the above terms, like cards with thumb and title. I can do it via shortcode or custom block, but the problem is that at this point, I'm going to repeat the above query, which I already did in page template.

Is there a way to simply reuse $children_terms on the custom block or shortcode?

Or, even more impossible: is there a way to split gutenberg output in 2 pieces, so that I can place standard 'manual' code in the middle?


Solution

  • I didn't find any way to pass page ($children_terms, see above) variables to a shortcode from gutenberg. But I solved with an 'empty' (where I mean: with no custom fields) acf block and passing $children_terms from the page template with set_query_var, a wordpress api function.