Filter query loop by two Relationships?

Support MB Relationships Filter query loop by two Relationships?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #41512
    OlivierOlivier
    Participant

    Hi all,

    I'm using a code snippet to filter a query loop with a Relationship as detailed here: https://support.metabox.io/topic/filter-query-loop-by-relationship/#post-40076

    The query appears on a Provider page (CPT). On that same page I have another query pulling from 2 different post types: Posts and Resources (another CPT). Each of these 2 post types has a Relationship set up with the Provider page the queries are on.

    Is it possible to modify the code snippet linked above to filter the query based on the Relationship per post type? i.e., when a Post is pulled by the query, filter by the post-provider Relationship, and when a Resource is pulled by the query, filter by the resource-provider Relationship.

    #41518
    PeterPeter
    Moderator

    Hello,

    I think it is possible. You can create an if statement to set the relationship arguments. For example:

    if (something) {
        $relationship = [
           'id' => 'post-provider',
           'to' => get_the_ID()
        ];
    } else {
        $relationship = [
           'id' => 'resource-provider',
           'to' => get_the_ID()
        ];
    }

    then assign the value to the relationship key

            return array_merge( $query_args, array(
                'relationship' => $relationship
            ) );
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.