-
Hi there, please can you tell me if it is possible to query and display results from an ACF Relationship field? As I cannot see it as an option. Please let me explain my setup:
– I have a custom post type of slug ‘profile’
– I have a custom taxonomy of slug ‘profile-tag’ which is assigned to the ‘profile’ post type
– I have a custom taxonomy of slug ‘type’ which is assigned to the core ‘post’ post type
– I have created a relationship field of Field Name ‘linked_consulting_reports’ which is displayed if a ‘profile’ post has a ‘profile-tag’ term of ‘consultant’
– I have also created a relationship field of Field Name ‘linked_consultants’ which is displayed if a ‘post’ (so the core WP post type of ‘post) has a ‘type’ term of slug ‘consulting-report’
– both the ‘linked_consulting_reports’ and ‘linked_consultants’ relationship fields have a bidirectional relationship enabled with each otherand here is what I would like to do if possible:
– I am creating a single ‘post’ which has a ‘type’ taxonomy term of ‘consulting-report’
– In that post I would like to display any ‘profile’ posts which are related to that post via the linked_consultants relationship fieldHowever, when I try to setup a Generate Blocks Query Block – I cannot see any option to load this dynamically.
Please can you explain if this is possible, and if so, how to do it?
Many thanks in advance for your help,
Keith
-
Hi Keith,
There is no built-in options for relationship field type.
It requires custom coding to alter the query loop output. Try this method:
1. Add a CSS class to the Grid block of the query loop, eg.
related-profiles
.
Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/2. Set post type and post per page in the query loop.
3. Add this PHP code:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'related-profiles' ) !== false && ! is_admin() ) { // get the relate post ID $current_post = get_the_ID() ; $relationship = get_field( "linked_consultants", $current_post ); // Merge the current $query_args with the new args return array_merge( $query_args, array( 'post__in' => $relationship, ) ); } return $query_args; }, 10, 2 );
Adding PHP: https://docs.generatepress.com/article/adding-php/
Let me know if this helps!
- You must be logged in to reply to this topic.