1

I am trying to write a Buddypress Query (similar to WP Query).

(1) It must pull all activity entries from a specific user id; regardless of meta mentioned in number 2.
(2) It must also pull activity entries marked with a certain meta value, regardless of the user who created.

Here is my current code for activity arguments:

    $activity_args = [
        'per_page' => 5,
        'show_hidden' => true,
        'filter_query' => [ 
            'relation' => 'AND',
            [
                'relation' => 'AND',
                [
                    'column' => 'user_id',
                    'value' => $user,
                    'compare' => '='
                ],
                [
                    'column' => 'component',
                    'value' => 'groups',
                    'compare' => '='
                ],
                [
                    'column' => 'type',
                    'value' => 'activity_update',
                    'compare' => '='
                ],
                [
                    'column' => 'item_id',
                    'value' => $group_id,
                    'compare' => '='
                ]
            ]
        ],
        //'meta_query' => [ [
            //'key' => 'share_guardian',
            //'value' => 'true',
            //'compare' => '='
        //] ]
    ];

If I comment out the meta query, number 1 is satisfied.
If I comment out the filter query, number 2 is satisfied.

How do I combine them into a single query?

1 Answer 1

1

Okay, I muddled around until I found what I feel is the best solution. Please feel free to offer any constructive criticism. I concluded this was not possible with a single query, so I broke it into two queries.

The basic idea is to get all items matching each query, loop them to build an array of id's, then perform a third query from just the id's. While this is probably not the most performant solution; it does give back what I need, in the correct order, maintains pagination, etc.

First query gets all activity entries of a specific user (from a specific group in my case):

$firstQueryArgs = [
    'show_hidden' => true,
    'posts_per_page' => -1,
    'filter' => [
        'user_id' => $user,
        'object' => 'groups',
        'action' => 'activity_update',
        'primary_id' => $group_id
    ]
];

Second query gets all activity items with a specific meta value:

$secondQueryArgs = [
    'show_hidden' => true,
    'posts_per_page' => -1,
    'filter' => [
        'object' => 'groups',
        'action' => 'activity_update',
        'primary_id' => $group_id
    ],
    'meta_query' => [ [
        'key' => 'share_guardian',
        'value' => 'true',
        'compare' => '='
    ] ]
];

Now, perform the queries, loop the results, and build an array of id's:

// Get queries
$firstQuery = bp_activity_get( $firstQueryArgs );
$secondQuery = bp_activity_get( $secondQueryArgs );

// Loop each query and set ids
$ids = [];

foreach( $firstQuery['activities'] as $key => $act ) {

    $ids[] = $act->id;
}
foreach( $secondQuery['activities'] as $key => $act ) {

    $ids[] = $act->id;
}

Finally, I can perform the main query:

// Perform final query by ids
$finalQueryArgs = [
    'show_hidden' => true,
    'per_page' => 5,
    'in' => $ids
];

$activity = bp_activity_get( $finalQueryArgs );

This is working well for my needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.