How to query for custom field within Gutenberg block from outside of that post? [closed]

I created a custom field inside of a custom Gutenberg block (using ACF). It is a Post Object field that accepts multiple post IDs. When inside that post, I can pull the data just fine and everything is working– I can loop through the IDs and output the correct information.

Now, from other posts I want to use WP_Query to find posts that are using that custom block and custom field and are related to the current post, but I can’t figure it out. If it were a typical custom field, I’d simply use a meta_key. But this seems more complex because the data is somewhere inside the post_content not the meta where standard custom fields live.

I know my example uses ACF, but I would have this same question whether I created the custom block and field manually or using ACF.

Basically I want to edit a Case Study post and correlate it to different industries (which are other posts). Then, on those individual industry pages I want to query and cross-link related Case Studies.

ACF Gutenberg block with Post Object custom field

Can someone point me in the right direction?

1 Answer
1

This is a bit of a work around, but you could create a Post Meta Block with the Block API, which simply sets a post meta value for a post.
Then you could add this block every time you add your ACF custom block, enabling you to query all posts with the ACF custom block.

This tutorial is a great place to start: https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/metabox/meta-block-1-intro/

Edit: You could also add an ACF-Field on post_type level, which is a lot easier. Set it to not allow null and it wouldn’t be possible to forget this field.

Leave a Comment