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

Issue

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.

Answered By – Luca Reghellin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published