How to exclude listings by certain users

On our site we let users block eachother and if user A blocks user B then user B should not see listings posted by user A.
I have tried to use the filter hook like this:

add_filter( 'hivepress/v1/models/listing', 'exclude_listings_from_blocked_users', 10, 2 );

function exclude_listings_from_blocked_users( $props, $model ) {
    $current_user_id = get_current_user_id(); // Get the current viewer (user B)

    // Get the list of users who have blocked the current user
    $blocked_by_users = bb_moderation_get_blocked_by_user_ids( $current_user_id );

    // Ensure the list excludes the current user (just in case)
    if ( ! empty( $blocked_by_users ) && in_array( $current_user_id, $blocked_by_users ) ) {
        $blocked_by_users = array_diff( $blocked_by_users, [ $current_user_id ] );
    }

    if ( ! empty( $blocked_by_users ) ) {
        // Exclude listings from users who have blocked the current user
        $props['args']['author__not_in'] = $blocked_by_users;
    }

    return $props;
}

But that doesn’t work. It won’t exclude any listings by user A even though the id of user A is included in the array $blocked_by_users.

I would be very grateful for some hints how to accomplish this. :slight_smile:

Hi,

I recommend using pre_get_posts hook instead, this way you can check if it’s the listing query, and it’s front-end, and then exclude specific post IDs based on the current user, via the post__not_in argument.

Hope this helps

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.