Skip to:
Content
Pages
Categories
Search
Top
Bottom

Search / Filter Activity by Meta


  • btees
    Participant

    @btees

    I’ve added a category option to activity updates using the following code:

    // Add Cat to Buddypress Activity Updates 
    
    function my_custom_activity_meta_stuff( $content, $user_id, $activity_id ) {
    	bp_activity_update_meta( $activity_id, 'bpcat', $_POST['bpcat'] );
    }
    add_action( 'bp_activity_posted_update', 'my_custom_activity_meta_stuff', 10, 3 );
    
    function add_bpcat_form_func(){
      global $wpdb;
      $result = $wpdb->get_results ( "SELECT * FROM wp_bp_xprofile_fields WHERE id='2'" );
        foreach ( $result as $print )   { ;?>
    	<div class="bpcat <?php echo $print->id;?>">
    		In Search Of: <select name="bpcat" id="bpcat">
    		  <option value="" disabled selected>Optional</option>
      <?php $options = $wpdb->get_results ( "SELECT * FROM wp_bp_xprofile_fields WHERE parent_id='$print->id'" );
          foreach ( $options as $option )   { ;?>
        	
        <option class="<?php echo $option->name;?>" name ="field_<?php echo $print->id;?>_match_any[]"value="<?php echo $option->name;?>"><?php echo $option->name;?></option>
              <?php } ;?> 
    		</select>
    	</div>
     <?php }
    } ; ?>
    <?php
    add_action('bp_activity_post_form_options', 'add_bpcat_form_func');

    What I’d like now is to be able to either filter the sitewide activity feed by category or include the activity metas in what is search by /common/search/search-form.php

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

  • Venutius
    Moderator

    @venutius

    The sitewide activity filter only works on activity types and the default search only looks in the activity content so that’s not a simple ask.


    btees
    Participant

    @btees

    Got it sorted.

    I set up a new search-form.php in my child theme:

    <div class="<?php bp_nouveau_search_container_class(); ?> bp-search" data-bp-search="<?php bp_nouveau_search_object_data_attr() ;?>">
    <form action="" method="get" class="bp-dir-search-form" id="<?php bp_nouveau_search_selector_id( 'search-form' ); ?>">
    <select onchange="this.form.submit()" id="<?php bp_nouveau_search_selector_id( 'search' ); ?>" name="<?php bp_nouveau_search_selector_name(); ?>">
    		  <option value="" disabled selected>Filter by Industry</option>
    		  <option value="">All</option>
      <?php  $result = $wpdb->get_results ( "SELECT * FROM wp_bp_xprofile_fields WHERE id='2'" );
      foreach ( $result as $print );
      $options = $wpdb->get_results ( "SELECT * FROM wp_bp_xprofile_fields WHERE parent_id='$print->id'" );
          foreach ( $options as $option )   { ;?>
        	
        <option class="<?php echo $option->name;?>" name ="field_<?php echo $print->id;?>_match_any[]"value="<?php echo $option->name;?>"><?php echo $option->name;?></option>
              <?php } ;?> 
    		</select>
    </form>
    </div>
    

    And then Ravi over at BuddyDev figured out how to change what the search applies to:

    // Activity Search by Category
    
    function buddydev_filter_activities_query_args( $r ) {
    
    	$searched_category = empty( $r[ 'search_terms' ] ) ? false : $r[ 'search_terms' ];
    
    	if ( ! $searched_category ) {
    		return $r;
    	}
    
    	$r['search_terms'] = false;
    
    	if ( empty( $r['meta_query'] ) ) {
    		$r['meta_query'] = array(
    			array(
    				'key'     => 'bpcat',
    				'value'   => $searched_category,
    			)
    		);
    	} else {
    		array_push( $r['meta_query'], array(
    			'key'     => 'bpcat',
    			'value'   => $searched_category,
    		) );
    	}
    
    	return $r;
    }
    add_filter( 'bp_after_has_activities_parse_args', 'buddydev_filter_activities_query_args' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar