Filter query loop by Relationship

Support MB Relationships Filter query loop by RelationshipResolved

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #40063
    OlivierOlivier
    Participant

    Hello,

    Trying again to get some help with Relationships... I have an event and provider 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

    #40072
    PeterPeter
    Moderator

    Hello,

    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/#posts

    #40076
    OlivierOlivier
    Participant

    Hi 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.

    #40090
    PeterPeter
    Moderator

    Hello,

    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.

    #40111
    OlivierOlivier
    Participant

    Peter, 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?

    #40117
    PeterPeter
    Moderator

    Hello,

    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.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.