0

I have a profile field with checkboxes where users can check their "interests". I'd like to customize the activity feed by scoping it down with a search string including the users interests. Following the search term method here (the example with "nap"): http://premium.wpmudev.org/blog/how-to-customize-the-buddypress-activity-loop/

I have this at the top of activity-loop.php:

<?php
  $activity_search_terms = "&search_terms=";
  if ( $interests = xprofile_get_field_data( 'Interests', bp_loggedin_user_id() ) ) {
    foreach ( $interests as $interest ) {
      $activity_search_terms .= strtolower($interest) . "+";
    }
  }
?>

<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . rtrim($activity_search_terms, '+') ) ) : ?>

This would work perfectly if the activity search were using an OR query instead of an AND query. Right now if a user clicks "yoga" and "crossfit" as interests, the activity feed is only posts with BOTH those terms instead of EITHER.

I installed Relevanssi and have it set to OR query, but it doesn't appear to apply to the activity search. Is there a way to force an OR query for bp_has_activities?

Using WP 4.1 and BP 2.1.1

UPDATE: I have it working via modifying BuddyPress bp-activity\bp-activity-classes.php around L.346 to write a LIKE statement for each search term. If you can tell me how to achieve the same thing in a plugin (without having to avoid $wpdb->prepare), the bounty is yours!

// Searching
if ( $search_terms ) {
  $search_terms_array = explode(' ', $search_terms);
  // WAS: $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%';

  $search_terms_like = '';
  foreach( $search_terms_array as $term)
    $search_terms_like .= 'a.content LIKE \'%' . bp_esc_like( $term ) . '%\' OR ';
  $where_conditions['search_sql'] = rtrim($search_terms_like, ' OR ');
  // WAS: $where_conditions['search_sql'] = $wpdb->prepare( 'a.content LIKE %s', $search_terms_like);
}

2 Answers 2

0

Take a look at this filter:

$where_conditions = apply_filters( 'bp_activity_get_where_conditions', $where_conditions, $r, $select_sql, $from_sql, $join_sql );

In \buddypress\bp-activity\bp-activity-classes.php ~L. 418

4
  • I saw around that code before, and figured swapping AND for OR on line 421 would work. That didn't do what I thought it would. I'm not sure what to do with the filter you mentioned. Would you mind telling me how I could work that into the code in the question (in activity-loop.php)?
    – Archonic
    Feb 4, 2015 at 23:37
  • L. 421 is for ALL the where conditions! Use the filter hook. Make your adjustments to $where_conditions for Search and return it. Use var_dump on the variables so you can see what they are. codex.wordpress.org/Function_Reference/add_filter
    – shanebp
    Feb 5, 2015 at 0:26
  • I'm a big noob, and don't really know how to add that filter. The var_dump is showing that the change needs to be in search_sql. It's currently [search_sql] => a.content LIKE '%outdoors yoga %' and needs to be [search_sql] => a.content LIKE '%outdoors%' OR LIKE '%yoga%'.
    – Archonic
    Feb 5, 2015 at 1:46
  • I've got it working via modifying buddypress\bp-activity\bp-activity-classes.php around L.346. If you can tell me how to achieve the same thing in a plugin, I'll place a 50 bounty :)
    – Archonic
    Feb 6, 2015 at 1:48
0

I previously had a solution which edited core files. Here's a solution you can drop into functions.php or build a plugin with:

// Customize activity filters
add_filter( 'bp_activity_get_where_conditions', 'my_activity_filters', 1, 1 );
function my_activity_filters($search) {

  $search_terms = explode( "%", $search["search_sql"] )[1];
  $search_terms_array = explode(' ', $search_terms);
  $search_terms_like = '';

  if( !empty( $search_terms_array ) ) {           // Searching for something specific
    foreach( $search_terms_array as $term)
      $search_terms_like .= 'a.content LIKE \'%' . bp_esc_like( $term ) . '%\' OR ';
    $my_search = rtrim($search_terms_like, ' OR ');
  } #else if(  ) {                       // Not searching for something specific, but user has interests, show those

  #} else {                              // Not searching for something specific, and user has no interests, show everything

  #}

  $search["search_sql"] = $my_search;

  return $search;

  #echo "<hr>";
  #echo "<strong>Search terms:</strong> $search_terms<br><br>";
  #echo "<strong>New search terms:</strong> $my_search<br><br>";
  #echo "<hr>";
}

Not production ready by any means but uncomment the echos and read the comments and you'll be able to get what you need.

3
  • Can I seriously not award myself the bounty? No one else answered in a specific enough manner.
    – Archonic
    Feb 13, 2015 at 2:01
  • You shouldn't modify core files Jun 1, 2015 at 20:06
  • Agreed. Posted a solution that doesn't edit core files.
    – Archonic
    Jun 3, 2015 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.