Just guessing, but try changing the filter priority from 12 to 10 or maybe 50, etc.
http://hookr.io/filters/bp_ajax_querystring/
I’ve tried a ton of numbers all the way up to 999. Nothing works.
bp_ajax_querystring is… complicated.
Perhaps there is another approach… What are you trying to filter?
I have a plugin that allows user to block themselves from the member page. It is currently working with regular search but not for the AJAX search.
AJAX search involves javascript – that is probably the issue.
There might be a way to accomplish your task via that filter… if you find it, please share it here.
For blocking aka exclude, I use this action hook: bp_pre_user_query_construct
For you, try something like:
function fencer_members_query( $query_array ) {
$exclude_ids = // collect all the member ids that don't want to appear, as an array
if ( ! empty( $exclude_ids ) )
$query_array->query_vars['exclude'] = $exclude_ids;
}
add_action( 'bp_pre_user_query_construct', 'fencer_members_query', 1, 1 );
Out of interest can we see your my_function()
definition?
shanebp – I have another function that is using the bp_pre_user_query_construct hook. It is not run when AJAX search is run. In my research this is normal and bp_ajax_querystring is the way to go for these cases.
Henry Wright – Here is the code and hook exactly how it is in my plugin file:
function sbpp04_hide_from_ajax_search( $query_string, $object ){
if( !empty( $query_string ) ){
$query_string .= "&";
}
$query_string .= "exclude=2";
return apply_filters( 'sbpp04_hide_from_ajax_search', $query_string, $object );
}
add_filter( 'bp_ajax_querystring', 'sbpp04_hide_from_ajax_search', 50, 2 );
Just guessing…
Instead of a filter, try using the action hook:
add_action( 'bp_legacy_theme_ajax_querystring', 'sbpp04_hide_from_ajax_search', 50, 2 );
https://codex.buddypress.org/developer/function-examples/bp_ajax_querystring/
btw – the example function I posted above works to exclude on ajax search too – at least for me.
No matter which option, filter or action, I use the code isn’t called when the AJAX search is run. Is there a particular time when I need to add the action or filter? Does it matter that I’m doing this through a plugin?
Inside your plugin try adding:
add_action( 'init', function() {
add_filter( 'bp_ajax_querystring', 'your_function', 10, 2 );
}
Of course, also define your function:
function your_function( $ajax_querystring, $object ) {
// Code here.
return $ajax_querystring;
}
Still nothing, I’m using XDebug to see if the function is being called. On the first page load the function gets called. After that I catch the core bp_ajax_querystring is getting called but not my function. It is really frustrating. If anyone knows of a plugin that uses it that I could review that would be helpful also.
So I moved my function into the functions.php file in my theme and now it is working with AJAX. It is something about being in my plugin that is causing it run only when the page is initially loaded. Any ideas?
Did you try putting it in the plugin loader?
iow. the file that starts with the plugin meta info.
Remember that wp ajax requires access to wp-admin
That did the trick. Thank you, I have thought about that a ton of times but never tried it. I really appreciate the help.
Is there anyway to be able to put that function in the other file? It would be nice to keep it with the others.