Skip to:
Content
Pages
Categories
Search
Top
Bottom

new query arg


  • Andrew
    Participant

    @snd26

    This code allows you to sort activity from most favorited activity to least favorited activity.

    class BP_Loop_Filters {
    	// Constructor
    	public function __construct() {
    		$this->setup_filters();
    	}
    
    	// Filters
    	private function setup_filters() {
    		add_filter( 'bp_activity_get_user_join_filter', array( $this, 'order_by_most_favorited' ),     10, 6 );
    		add_filter( 'bp_activity_paged_activities_sql', array( $this, 'order_by_most_favorited'),      10, 2 );
    		// Maybe Fool Heartbeat Activities!
    		//add_filter( 'bp_before_activity_latest_args_parse_args', array( $this, 'maybe_fool_heartbeat' ), 10, 1 );
    	}
    	
    	/**
    	 * Ninja Warrior trick to reorder the Activity Loop
    	 * orders by most likes highest to least
    	 */	 
    	public function order_by_most_favorited( $sql = '', $select_sql = '', $from_sql = '', $where_sql = '', $sort = '', $pag_sql = '' ) {
    		$r = $select_sql;
    		if ( empty( $r['meta_query'] ) || ! is_array( $r['meta_query'] ) ) {
    			return $sql;
    		} else {
    			$meta_query_keys = wp_list_pluck( $r['meta_query'], 'key' );
    			if ( ! in_array( 'favorite_count', $meta_query_keys ) ) {
    				return $sql;
    			}
    			preg_match( '/\'favorite_count\' AND CAST\((.*) AS/', $sql, $match );
    			if ( ! empty( $match[1] ) ) {
    				$sql = str_replace( 'ORDER BY a.date_recorded', 'ORDER BY '. $match[1] .' + 0', $sql );
    			}
    		}
    
    		return $sql;
    	}
    }
    
    // 1, 2, 3 go !
    function bp_loop_filters() {
    	return new BP_Loop_Filters();
    }
    add_action( 'init', 'bp_loop_filters' );

    with query args like this:

    	// order activity by favorited. 
    	$query_args = array(
    		'meta_query' 	=> 	array(
    			array(
    				'key'     => 'favorite_count',
    				'value'   => 1,
    				'type'    => 'numeric',
    				'compare' => '>='
    			),
    		),
    	);

    Does anyone know how to add a new parameter argument for this? instead of favorite_count, I would like something like orderby_favorite_count.

    The reason is, I’ve got custom pages where I like to manipulate the activity loop in a variety of ways, and if I don’t use that class BP_Loop_Filters, then this query args code:

    	$query_args = array(
    		'meta_query' 	=> 	array(
    			array(
    				'key'     => 'favorite_count',
    				'value'   => 1,
    				'type'    => 'numeric',
    				'compare' => '>='
    			),
    		),
    	);

    will get activities that have been favorited at least x amount of times, but ordered by date_recorded.

    So I would like to be able to
    1. order activities from most favorited, to least favorited, and
    2. order activities by date with activities that has been favorited at least x amount of times.

    At the moment I can only do one or the other.

    Any help appreciated.

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