我有一个带有复选框的配置文件字段,用户可以在其中检查他们的“兴趣”。我想通过一个包含用户兴趣的搜索字符串来定制活动提要。遵循此处的搜索词方法(带有“nap”的示例):http://premium.wpmudev.org/blog/how-to-customize-the-buddypress-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, \'+\') ) ) : ?>
如果活动搜索使用OR查询而不是AND查询,这将非常有效。现在,如果用户点击“瑜伽”和“交叉健身”作为兴趣,那么活动提要将只包含这两个术语,而不是其中任何一个。
我安装了Relevanssi并将其设置为或query,但它似乎不适用于活动搜索。是否有方法强制或查询bp_has_activities
?
使用WP 4.1和BP 2.1.1
UPDATE:我通过修改BuddyPress使其工作bp-activity\\bp-activity-classes.php
在L.346左右为每个搜索词写一个类似的语句。如果你能告诉我如何在插件中实现同样的功能(无需避免$wpdb->prepare
), 赏金是你的!
// 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);
}
最合适的回答,由SO网友:Archonic 整理而成
我以前有一个编辑核心文件的解决方案。这里有一个可以放入函数的解决方案。php或使用以下内容构建插件:
// 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>";
}
没有任何生产准备就绪,但请取消对Echo的注释并阅读注释,您将能够获得所需的内容。