Skip to:

[Resolved] Limiting Activities by User’s City

  • puffyjoe


    I’ve added a CITY profile field to user.

    I now want to filter the global activities feed by this. So I need to save the users’ city value when they create an update.

    So far, I can filter by content, grab the city from the user, and filter by that city and if the text is in the content it works.

    But it would be nicer to be able to use a meta param on the update, set that to city, and filter by that. I just cant find the documentation.

    right now this is what I am doing:

    function my_filter_activity( $loop ) {
        $user_id = bp_loggedin_user_id();
        $city = xprofile_get_field_data('9', $user_id);
        $loop['search_terms'] =  $city ;
        return $loop;
    add_filter( 'bp_after_has_activities_parse_args', 'my_filter_activity' );

    The main help I need is … how can I store the users city. I guess it’s ok if it shows as a header on their update like:
    ” City: Orlando
    <update text>

    but better as a meta searchable attribute.

    Then eventually (Part 2) I’d like to have a dropdown where the user can override their registered city and the value of the dropdown in the update would be saved instead.

    Help! Thanks!

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

  • shanebp


    Please use the code button.

    Store your meta in the bp_activity_meta table when an activity entry is created.
    Then you can use an Activity Meta Query to filter activities.

    Add custom filters to loops and enjoy them within your plugin



    OK great tutorial I will work through it.

    Thanks so much!



    I just wanted to say I got it working. this is my code to filter by the city I save in meta

    	public function activity_querystring_filter( $query_string = '', $object = '' ) 
    	    if( $object != 'activity' )
    		return $query_string;
    	    // You can easily manipulate the query string
    	    // by transforming it into an array and merging
    	    // arguments with these default ones
    	    $args = wp_parse_args( $query_string, array(
    		'action'  => false,
    		'type'    => false,
    		'user_id' => false,
    		'page'    => 1
    	    ) );
    	    if( bp_is_user() )
    	    	    $args['user_id'] = bp_displayed_user_id();
    	   // try to use args  $user_id = bp_loggedin_user_id();
    	    $user_id = bp_loggedin_user_id();
    	    $city = xprofile_get_field_data('9', $user_id);
    	    //   $city = xprofile_get_field_data('9', $args['user_id']);  //not sure if this is set right but could work
    	    // An activity meta query <img src="" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;">
    	    $args['meta_query'] = array(
    		 	 /* this is the meta_key you want to filter on */
    			'key'     => 'city',
    			/* You need to get all values that are >= to 1 */
    			'value'   => $city,
    			'type'    => 'CHAR',
    			'compare' => '='
    	    $query_string = empty( $args ) ? $query_string : $args;
    	    return apply_filters( 'bp_plugin_activity_querystring_filter', $query_string, $object );
    	}//end method activity_querystring_filter 



    @puffyjoe, thank you for sharing your solution ! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Limiting Activities by User’s City’ is closed to new replies.
Skip to toolbar