BuddyDev

Search

[Resolved] Search Activity Meta


  • Participant
    Level: Initiated
    Posts: 10
    Barry Grennan on #22994

    I’ve added the ability for users to add a category to their activity posts 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');

    Now I want those categories to be searchable in the activity feed.

    To this end I’ve created a custom template in my child theme for common/search/search-form.php

    <?php
    /**
     * BP Object search form
     *
     * @since 3.0.0
     * @version 3.1.0
     */
    ?>
    
    <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 Category</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>

    When this is used it results in a url with a querystring like:
    https://mydomain.com/activity/?activity_search=CATEGORYSELECTED

    But no activity updates are shown because the activity_search query searches activity content and not meta

    I want the search to apply to my activity meta, not the content of the activity, how would I go about doing this?


  • Keymaster
    (BuddyDev Team)
    Posts: 11216
    Brajesh Singh on #22996

    Hi Barry,
    You will need to use ‘meta_query’ for activity to search based on meta.

    Are you storing the category name in meta?(It seems you are storing name).

    The correct way to do it will be to hook to bp_after_has_activity_parse_args and set meta_query if your conditions are matched.

    I will be cleaning up your search form as you don’t need class/name for option and you can use a different field name for select to reliably detect if the search was for your category.

    Regards
    Brajesh


  • Participant
    Level: Initiated
    Posts: 10
    Barry Grennan on #22998

    Hi Brajesh, Thanks for the reply!

    Yes, I’m storing the category name in meta with
    bp_activity_update_meta( $activity_id, 'bpcat', $_POST['bpcat'] );

    So in my database there’s a table wp_bp_activity_meta

    If a user creates an activity update that they’ve given the category “Finance”, an entry is added to that table with a meta_key of ‘bpcat’ and a meta_value of ‘Finance’

    How would I go about hooking to bp_after_has_activity_parse_args and setting meta_query if the conditions are matched?


  • Keymaster
    Level: Guru
    (BuddyDev Team)
    Posts: 678
    Ravi on #23000

    Hello Barry,

    Try the following code and let me know if it works or not.

    
    function buddydev_filter_activities_query_args( $r ) {
    
    	$searched_category = empty( $_REQUEST[ 'activity_search' ] ) ? false : $_REQUEST[ 'activity_search' ];
    
    	if ( ! $searched_category ) {
    		return $r;
    	}
    
    	$r['search_terms'] = false;
    
    	$category_args = array(
    		'key'     => 'bpcat',
    		'value'   => $searched_category,
    	);
    
    	$r[ 'meta_query' ] = empty( $r['meta_query'] ) ? array( $category_args ) : array_push( $r['meta_query'], $category_args );
    
    	return $r;
    }
    add_filter( 'bp_after_has_activities_parse_args', 'buddydev_filter_activities_query_args' );
    

    Regards
    Ravi


  • Participant
    Level: Initiated
    Posts: 10
    Barry Grennan on #23009

    Hi Ravi,

    Thanks for the reply.

    Unfortunately your code didn’t work for me.

    mydomain.com/activity/?activity_search=CATEGORY

    Still returns “Sorry, there was no activity found. Please try a different filter.”

    But
    mydomain.com/activity/?activity_search=KEYWORD

    Returns posts where the keyword is in the activity content.


  • Keymaster
    Level: Guru
    (BuddyDev Team)
    Posts: 678
    Ravi on #23019

    Hello Barry,

    Thank you for the acknowledgement. Please give it a try the following code and let me know if it works or not.

    
    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' );
    
    

    Regards
    Ravi


  • Participant
    Level: Initiated
    Posts: 10
    Barry Grennan on #23025

    Awesome. That worked.

    Thanks a million Ravi. I’ll have to buy you a pint/coffee!


  • Keymaster
    Level: Guru
    (BuddyDev Team)
    Posts: 678
    Ravi on #23029

    Hello Barry,

    Thank you for the acknowledgement. I am glad that code worked.

    Regards
    Ravi

The topic ‘ [Resolved] Search Activity Meta’ is closed to new replies.

This topic is: resolved
Subscribe