This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Can you please add additional query_add_select/join/where filters?

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Can you please add additional query_add_select/join/where filters? Reply To: Can you please add additional query_add_select/join/where filters?

#35149
Ernest MarcinkoErnest Marcinko
Keymaster

Hi,

Finally I had the time to inspect the source code, as I recalled I had some sort of a solution for this already, I just was not sure.

So initially I have added the query_add_select/join/etc.. hooks, but later on, when the plugin was extended, we figured it might be a bit too much of a code overhead, so we wanted to merge all of these into a single hook. The current solution is, that extra query arguments can be added via the asp_query_args filter – and the good news is, that it is also available for BuddyPress. Unfortunately I completely forgot to document this (and forgot about it as well), and the older version of the filters was not documented on purpose (so we can switch to this new solution, and people won’t use the old ones).

So, all you need is to use the asp_query_args hook. And then, there are these array keys for the $args variable (taken from the source):

// ----------------------------------------------------------------
// 10. QUERY FIELDS
// ----------------------------------------------------------------
'cpt_query' => array(
	'fields' => '',
	'join' => '',
	'where' => '',
	'orderby' => '',
	'groupby' => ''
),
'term_query' => array(
	'fields' => '',
	'join' => '',
	'where' => '',
	'orderby' => '',
	'groupby' => ''
),
'user_query' => array(
	'fields' => '',
	'join' => '',
	'where' => '',
	'orderby' => '',
	'groupby' => ''
),
'attachment_query' => array(
	'fields' => '',
	'join' => '',
	'where' => '',
	'orderby' => '',
	'groupby' => ''
),
'buddypress_groups_query' => array(
	'fields' => '',
	'join' => '',
	'where' => '',
	'orderby' => '',
	'groupby' => ''
),
'buddypress_activities_query' => array(
	'fields' => '',
	'join' => '',
	'where' => '',
	'orderby' => '',
	'groupby' => ''
),

From here, this gets very simple. In a hook on asp_query_args, you can basically do anything:

global $wpdb;
$args['buddypress_activities_query']['fields'] = 'my_custom_row as row,';
$args['buddypress_activities_query']['join'] = "LEFT JOIN my_table on $wpdb->prefix" . ".bp_activity.user_id";
$args['buddypress_activities_query']['where'] = "AND (my_field.xyz = $wpdb->prefix" . ".bp_activity.user_id)";

..and so on, I’m sure you get the point.