BuddyDev

Search

Search Activity Meta

Tagged: 

  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #35232

    Hi! I wish you have a great year
    I’m adapting the search code of activities for meta_key.
    The error is in the line of $sql that i cant found the right way to resolve.
    I see the post https://buddydev.com/support/forums/topic/search-activity-meta/ but in this not use the sql.
    I’ve been looking in forums and nothing helps me, can you please guide me?

    `function sql( $search_term, $only_totalrow_count=false ){
    global $wpdb, $bp;
    $query_placeholder = array();

    $activity_table = $wpdb->prefix.”bp_activity”;
    $activity_meta_table = $wpdb->prefix.”bp_activity_meta”;
    $sql = “SELECT COUNT(*) FROM $activity_table a INNER JOIN $activity_meta_table am ON a.id = am.activity_id WHERE a.item_id = %s AND meta_key = ‘titulo’ OR meta_key = ‘descr”;

    $query_placeholder[] = ‘%’.$wpdb->esc_like( $search_term ).’%’;
    $sql = $wpdb->prepare( $sql, $query_placeholder );

    return apply_filters(
    ‘BBoss_Global_Search_Activities_sql’,
    $sql,
    array(
    ‘search_term’ => $search_term,
    ‘only_totalrow_count’ => $only_totalrow_count,
    )
    );
    }`

    The original code is

    `function sql( $search_term, $only_totalrow_count=false ){
    global $wpdb, $bp;
    $query_placeholder = array();
    $sql = ” SELECT “;
    if( $only_totalrow_count ){
    $sql .= ” COUNT( DISTINCT id ) “;
    } else {
    $sql .= ” DISTINCT a.id , ‘activity’ as type, a.content LIKE %s AS relevance, a.date_recorded as entry_date “;
    $query_placeholder[] = ‘%’.$wpdb->esc_like( $search_term ).’%’;
    }

    //searching only activity updates, others don’t make sense
    $sql .= ” FROM
    {$bp->activity->table_name} a
    WHERE
    1=1
    AND is_spam = 0
    AND a.content LIKE %s
    AND a.hide_sitewide = 0
    AND a.type = ‘activity_update’
    “;
    $query_placeholder[] = ‘%’.$wpdb->esc_like( $search_term ).’%’;
    $sql = $wpdb->prepare( $sql, $query_placeholder );

    return apply_filters(
    ‘BBoss_Global_Search_Activities_sql’,
    $sql,
    array(
    ‘search_term’ => $search_term,
    ‘only_totalrow_count’ => $only_totalrow_count,
    )
    );
    } `

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #35237

    Hi Jennifer ,
    Thank you for the question.
    Are you trying to use to with BuddyBoss Global search?

    if yes, I will suggest contacting them. Their search is not the normal activity query. So, the code from the linked url may not work.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #35240

    Hi Brajesh, if i use the search of buddypress, should i replace the search.php? Because i tried this code (search in meta_key “descr” and “titulo”) but i cant do it work
    And in the example, the user is using noveau theme.

     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'     => 'descr',
    				'value'   => $searched_category,
    			)
    		);
    	} else {
    		array_push( $r['meta_query'], array(
    			'key'     => 'titulo',
    			'value'   => $searched_category,
    		) );
    	}
    
    	return $r;
    }
    add_filter( 'bp_after_has_activities_parse_args', 'buddydev_filter_activities_query_args' );
    

    Thanks!!

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #35303

    Hi Jennifer,
    Thank you for the reply.
    The above code does not need you to change anything in your template. It hooks to the default activity searching and uses the search term to search in meta.

    Is the “titulo” field stored as meta key? Do you want to go for eact match(the above code only does exact match).

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #35333

    Hi Brajesh.
    Thank you for your reply.
    Yes, *title* and *descr* are meta key and i would like search if any word match. But i cant found how do it =(

    Regards,
    Jennifer

You must be logged in to reply to this topic.

This topic is: not resolved