Filter query loop by Relationship
Support › MB Relationships › Filter query loop by RelationshipResolved
- This topic has 5 replies, 2 voices, and was last updated 2 years, 3 months ago by
Peter.
-
AuthorPosts
-
December 30, 2022 at 6:39 AM #40063
Olivier
ParticipantHello,
Trying again to get some help with Relationships... I have an
event
andprovider
post types connected with a Relationship already established using the Builder.I've created a query loop on each Provider page to display Events, but I want to filter that query loop to only display Events that are related to that Provider.
Do you have an example of code for how to filter an existing query loop with Relationship? Note, I'm not trying to display the Relational information, I'm just trying to filter the loop so Events from other Providers than the current one (the one whose page I'm on) are filtered out.
Thank you
December 30, 2022 at 11:05 PM #40072Peter
ModeratorHello,
Can you please share the code that you create the query to display Event posts? Did you try to add the parameter
relationship
as in the documentation below to the query?
https://docs.metabox.io/extensions/mb-relationships/#postsDecember 31, 2022 at 5:03 AM #40076Olivier
ParticipantHi Peter,
I'm not sure how to find/share the code for the query, it is generated by the GenerateBlocks Pro plugin. That's why I don't know how to pass
relationship
parameters to it, and the documentation is not helping me (I see the documentation, I just don't know how to use it for my use-case).However I can share the code I'm currently using to filter the query based on a custom field (using Metabox' "Post" custom field to mimic a relationship):
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'only-provider-events' ) !== false ) { return array_merge( $query_args, array( 'meta_query' => array( array( 'key' => 'event_host', 'value' => get_the_ID(), 'compare' => '=', ) ) ) ); } return $query_args; }, 10, 2 );
Can I tweak this code snippet to pass
relationship
parameters?If I can get this to work, I'll be happy to go publish the solution on the GeneratePress/GenerateBlocks forums, as I know I'm not the only one struggling with this.
January 2, 2023 at 7:18 PM #40090Peter
ModeratorHello,
You can try to use this code
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'only-provider-events' ) !== false ) { return array_merge( $query_args, array( 'relationship' => [ 'id' => 'event_to_provider', // Pass your relationship ID 'to' => get_the_ID(), // You can pass object ID or full object ], ) ); } return $query_args; }, 10, 2 );
If it does not work, please generate the relationship to the PHP code and share it here. I will help you to check this.
January 4, 2023 at 1:37 AM #40111Olivier
ParticipantPeter, it works thank you so much! I've been struggling with this for weeks!
Can you clarify what you mean by "You can pass object ID or full object"? I understand object ID, but what is "full object"? Do you mean the object ID, and any content field within it?
January 4, 2023 at 11:05 PM #40117Peter
ModeratorHello,
It's the post/term/user object of WordPress. You can read more here https://developer.wordpress.org/reference/classes/wp_post/
As I said earlier (or another topic), you need to have a basic knowledge of PHP and WordPress coding to understand and use the full features of Meta Box.
-
AuthorPosts
- You must be logged in to reply to this topic.