Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filtering Activity Loop


  • nicolemb
    Participant

    @nicolemb

    My site is completely up to date on WP and BP. I would like to filter activity loops on a multisite and have created a must use plugin to filter the activity to include only what is happening on the current site, with all activity shown on the main site. I have this but it is currently filtering too much, only showing very limited activity feeds, so it seems like I am over filtering my results. Any suggestions on how to get this right? Do I also have to filter member loops to achieve this completely?

    add_action('bp_activity_posted_update', 'where_activity_from', 10, 3);
    function where_activity_from($content, $user_id, $activity_id)
    {
    bp_activity_add_meta($activity_id, '_source_blog', get_current_blog_id());
    }

    function bp_after_has_activities_parse_args_func($r)
    {
    $blog_id = get_current_blog_id();
    global $wpdb;
    $sql = $wpdb->prepare("SELECT activity_id FROM " . $wpdb->base_prefix . "bp_activity_meta WHERE meta_key=%s AND meta_value=%d", '_source_blog', $blog_id);

    $ids = array_values($wpdb->get_col($sql));
    if (empty($ids)) {
    $r[‘in’] = ‘-1’;
    } else {
    $r['in'] = $ids;
    }
    return $r;
    }

    if (get_current_blog_id() !== 1) {
    add_filter('bp_after_has_activities_parse_args', 'bp_after_has_activities_parse_args_func');
    }

Viewing 5 replies - 1 through 5 (of 5 total)

  • shanebp
    Moderator

    @shanebp

    Please use the code button when sharing code.

    I don’t have much experience with multisite, but you’re adding meta based on this hook:
    add_action('bp_activity_posted_update', 'where_activity_from', 10, 3);

    A lot of activity entries are not updates, so maybe that is why your filtered stream is so sparse.

    Sometimes it’s easier and faster to test a query directly in a db tool like phpmyadmin first.
    There may be an easier way to get blog-specific entries without adding meta.


    nicolemb
    Participant

    @nicolemb

    Thanks @shanebp – This is our problem. This whole solution hinges on adding meta with the blog id to EVERYTHING that could appear in an activity stream. (We also have Activity Plus Plugin active on one site and those posts seem to be missing getting the meta added)

    Is there something we hook to in order to add meta to all activity? we tried add_action('bp_activity', 'where_activity_from', 10, 3); but it’s still not adding meta to all.

    Or is there a better way to filter activity streams to only show activity from the current site on a multisite setup?

    Here’s our latest attempt at a must-use plugin, but not all activity is shown implying the meta values are not being added:

    <?php
    
    // Add Blog ID meta to activity posts
    add_action('bp_activity', 'where_activity_from', 10, 3);
    function where_activity_from($content, $user_id, $activity_id)
    {
        bp_activity_add_meta($activity_id, '_source_blog', get_current_blog_id());
    }
    
    // Filter specific sites for activity from that site only
    function bp_after_has_activities_parse_args_func($r)
    {
        $blog_id = get_current_blog_id();
        global $wpdb;
        $sql = $wpdb->prepare("SELECT activity_id FROM " . $wpdb->base_prefix . "bp_activity_meta WHERE meta_key=%s AND meta_value=%d", '_source_blog', $blog_id);
    
        $ids = array_values($wpdb->get_col($sql));
        if (empty($ids)) {
            $r['in'] = '-1';
        } else {
            $r['in'] = $ids;
        }
        return $r;
    }
    
    if (get_current_blog_id() !== 1) {
        add_filter('bp_after_has_activities_parse_args', 'bp_after_has_activities_parse_args_func');
    }

    shanebp
    Moderator

    @shanebp

    I’d try using this hook: do_action( 'bp_activity_add', $r ); from bp-activity/bp-activity-functions.php


    nicolemb
    Participant

    @nicolemb

    Thanks @shanebp. We tried this and a few other hooks and still have not been able to get the appropriate activity on the stream.

    It does seem like the issue with this mu-plugin solution is that not every bit of activity is getting assigned the site id in the meta data. So the filter ends up filtering out most of the activity we should see. Ultimately we are seeking a way to limit activity stream data to the site you are visiting without using multi-network plugins.

    We have a wordpress multisite network with buddypress NOT network activated but only activated on specific subsites.

    Do you or anyone have any suggestions on how to achieve our goals?


    Boone Gorges
    Keymaster

    @boonebgorges

    The hook that is the closest to the metal, so to speak, is bp_activity_after_save. So your callback would be something like:

    
    add_action('bp_activity_after_save', 'where_activity_from', 10, 3);
    function where_activity_from( $activity ) {
        bp_activity_add_meta( $activity->id, '_source_blog', get_current_blog_id() );
    }
    

    This ought to cover any activity item created by any plugin.

    The rest of your technique seems like it ought to work. If you wanted to leverage BP’s APIs a bit more (and avoid making direct SQL queries), you could modify meta_query instead of your SELECT FROM {$bp->activity->table_name_meta} WHERE.... But it comes down to pretty much the same thing 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Filtering Activity Loop’ is closed to new replies.
Skip to toolbar