Skip to:
Content
Pages
Categories
Search
Top
Bottom

activities sort plugin not working


  • panda1337
    Participant

    @panda1337

    <?php
    /**
     * Plugin Name: BuddyPress Reaction Sort 
     * Description: Sorts BuddyPress activities by reactions.
     * Version: 1.0
     * Author: Your Name
     */
    require_once( '/www/wwwroot/fsyz.club/wp-content/plugins/buddypress/bp-loader.php' );
    
    function bp_reaction_sort() {
    
      global $wpdb;
    
      $activities = bp_activity_get( array(
        'per_page' => 20    
      ) );
    
      // Debug
      echo "Query returned " . count($activities['activities']) . " activities <br>";
    
      $activity_reactions = array();
    
      foreach ( $activities['activities'] as $activity ) {
    
        $id = $activity->id;
    
        $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}iq_reaction_activity WHERE activity_id = $id" );
    
        $activity_reactions[(int)$id] = (int)$count;
    
      }
    
      arsort( $activity_reactions );
      var_dump( $activity_reactions  );
      return $activity_reactions;
    
    }
    add_filter( 'bp_has_activities', 'bp_replace_activities', 10, 2 );
    function bp_replace_activities( $has_activities, $activity_reactions ) {
    
      global $activity_reactions;
      $activity_reactions = bp_reaction_sort();
      $sorted_activities =  array_keys( $activity_reactions );
      
      $has_activities = false;
    
      buddypress()->activities->activities = $sorted_activities;
      var_dump($sorted_activities );
      $has_activities = true;
      return BP_Activity_Activity::get( array(
        'in' => $sorted_activities
      )); 
    
    }
    
    add_action( 'bp_before_directory_activity_list', 'bp_reaction_sort' );

    it output right order in $sorted_activities but the stream is in wrong order,with a warning:Creating default object from empty value anybody can help me ?

  • You must be logged in to reply to this topic.
Skip to toolbar