- This topic has 1 reply, 2 voices, and was last updated 3 years ago by
David.
-
AuthorPosts
-
March 5, 2023 at 7:00 pm #2556831
Dane
Hi David. New topic per request…
The site is for a martial arts dojo.
I have 3 custom post types – dojos, instructors and events.
In the events page I want to bring the event location (a dojo) into the event post. I have all the dojos as their own posts so have created an ACF field (event_dojo) with type ‘Relationship’ and filter that to my Dojo post type. All that works fine and shows up as expected in the post edit page (see image). Problem is nothing shows up when I try to pull it in via dynamic data. All the standard text is coming in fine via the same procedure – just all the relational stuff doesn’t work – and have tried all the ACF settings. Note that the dojo and event templates are custom block elements.
Also, and this may well be out of scope, I want to be able to use a date picker in an ACF field to pick when an instructor first started. Then I want to be able to use that output and use current date to yield a number for years of experience. I can do it in the old way of custom templates and ACF – just can’t get my head around how to do this stuff in filters.
March 6, 2023 at 2:48 am #2557142David
StaffCustomer SupportHi there,
to get data from the relationship requires a loop, so you can try using a GB Query Loop Block to do that.
1. Add a Query Loop block.
1.1 Set its Params for the post type and one post, and nothing else.
1.2 in its Grid Block -> Advanced > Additional CSS Classes add a class name, make a note as this will be used in the PHP snippet.
1.3. Add your headline block(s) and enable its dynamic data:
Data Source -> Current Post
Content Source -> Post Meta
POST META FIELD -> your ACF Field2. Now we need a PHP snippet to filter the parameters of that Query Loop:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { $relation = 'my_relationship_field'; $elementClass = 'my-class-name'; if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], $elementClass) !== false && ! is_admin() ) { // get the relate post ID $queried_object = get_queried_object () ; $relationship = get_field( $relation, $queried_object ); // Merge the current $query_args with the new args return array_merge( $query_args, array( 'post__in' => $relationship, ) ); } return $query_args; }, 10, 2 );Note the two variables in the code:
$relation = 'my_relationship_field'; $elementClass = 'my-class-name';Update them to your ACF Relationship field name, and the Additional CSS Class you added to the grid block.
-
AuthorPosts
- You must be logged in to reply to this topic.