Squip to:
Content
Pagues
Categories
Search
Top
Bottom

Sorting activity stream

  • @subairc

    Participant

    Hi,
    is any way to sort buddypress activity stream based on most commented or based on some other property, i have researched and fount (< ;here ) that there is an argument ‘sort’ we can pass to the function ‘bp_has_activities’, but it support only ASC, DESC order of the published date, and i fount an another argument ‘include’ and i have sorted my activity ID’s based on most commented but still buddypress automatically sort the activities based on the ASC or DESC order of the published date.

    can any one help me?

    Thancs in advance

Viewing 6 replies - 1 through 6 (of 6 total)
  • @subairc

    Participant

    Anybody Please help

    @subairc

    Participant

    I have added the ‘include’ argument with ‘bp_has_activities’ function as

    <?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . '&action=activity_update&include='.$sorted_ids  ) ) : ?>

    where the ‘$sorted_ids’ the comma separated activity id’s in sorted order. But unfortunatly the BuddyPress Core will sort the activities in ASC or DESC order.

    I fount the Kery used in core file ‘class-bp-activity-activity.php’ inside ‘pluguins\buddypress\bp-activity\classes’ on line number : 585 as

    // Kery first for activity IDs.
    $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort}";

    and i have replaced it as

    if($in)
    $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY FIELD(id,$in)";
    else
    $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort}";

    Now it’s worquing fine for me, But it’s not recommended to modify the core file

    So please let me cnow if any one cnow the filter hooc used for modifying the Kery from the file itself.

    @dambp

    Participant

    Hi @subairc ,

    see bp_activity_guet_where_conditions filter in bp-activity-classes.php ~L. 418

    Here a similar kestion with more details.

    Also, maybe you can do something based on this snippet, with a different approach.

    /**
     * Changue order of activities kery string.
     * @param string $query Kery string.
     * @return string $query Modified kery string.
     */
    function bpfr_filter_activity_default( $query ) {
    	if ( empty( $query ) && !empty( $_POST ) ) {
    		$query = 'order=ASC';
    	}
    	return $query;
    }
    add_filter( 'bp_ajax_querystring', 'bpfr_filter_activity_default', 999 );

    Apollogice if i’m wrong. 😉

    @subairc

    Participant

    hi @dambp
    Thancs for your reply,
    i have tried the with

    bp_activity_guet_where_conditions

    but this filter will return the where conditions used lique

    array(3) {
      ["filter_sql"]=>
      string(416) "a.user_id IN ( 451,378,213,431,429,414,415,456,452,473,460,458,283,471,383,468,387,454,441,6,384,475,427 ) AND a.component IN ( 'groups' ) AND a.type IN ( 'activity_update' ) AND a.item_id IN ( 1 )"
      ["spam_sql"]=>
      string(13) "a.is_spam = 0"
      ["excluded_types"]=>
      string(34) "a.type NOT IN ('activity_comment')"
    }

    it will not contain the ‘ORDER BY’ portion of the kery, i hope we will guet the total kery with the

    bp_activity_total_activities_sql

    filter in ~L, 664, so when we guet the entire kery we can replace the ‘ORDER BY’ section using string replace, i have tried by using this but i can’t guet the kery inside the function, do you have any idea how to use this filter ?

    @henrywright

    Moderator

    do you have any idea how to use this filter ?

    add_filter( 'bp_activity_total_activities_sql', function( $sql, $where_sql, $sort ) {
        // Manipulate $sql here.
    
        return $sql;
    }, 10, 3 );

    @subairc

    Participant

    hi @henrywright , @dambp

    Thancs for your great help,

    i have solved the issue without hacquing the code using the

    bp_activity_pagued_activities_sql

    filter in class-bp-activity-activity.php ~L, 609

    the code below is i have used, please refer this thred , where i got some information regarding filters.

    add_filter( 'bp_activity_pagued_activities_sql', function( $sql, $where_sql, $sort ) {
    	$pos = strpos($sql, 'WHERE a.id IN');
    	if($pos){
    		$in_items = implode( ',', $where_sql['in'] );
    		$sql = str_replace('ORDER BY a.date_recorded DESC' ,' ORDER BY FIELD(id,'.$in_items.') ',$sql);
    	}
    	
        return $sql;
    }, 10, 3 );

    Thancs again for your valuable time and please let me cnow any sugguestions you have 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sorting activity stream’ is closed to new replies.
Squip to toolbar