Adding attribute(s) to Requests Query

I’m trying to narrow down the Requests visible to a Vendor to include ONLY the services they have (Custom Requests attribute of type Checkboxes). At the end of the day, I’m intending my Query to ADD a bunch of criteria to exclude Requests that need services that the vendor doesn’t provide (also defined by a similar checkboxes attribute).

To get started, I’ve been trying to figure out how to leverage the add_action described here:

add_action('hivepress/v1/models/request/search',function($query,$fields) {
	$meta_query = array_filter( (array) $query->get( 'meta_query' ) );
	$meta_query['needs'] = [
		'key' => 'hp_needs',
		'type' => 'CHAR',
		'compare' => '=',
		'value' => 'Custom Development',
	];
	$query->set( 'meta_query', $meta_query );
}, 1000, 2
);

The code doesn’t appear to be firing at all (even if I put a die() statement, no impact). 1st step for me would be to get this to actually execute. Then, adding multiple conditions (something like needs NOT LIKE ‘%svcVendorDoesntOffer%’ AND needs NOT LIKE ‘%otherSvc%’)

Please try this PHP snippet. Please change checkboxes_taxonomy_slug on the taxonomy slug of the checkboxes request attribute, which it is possible to get when you click Edit Options in the attribute and it is possible to find taxonomy slug between ?taxonomy= and &post_type words in the address bar of your browser

But please note that it can require further customization. If you are not familiar with the code customization then please consider hiring someone for custom work https://fvrr.co/32e7LvY

add_action(
	'hivepress/v1/models/request/search',
	function($query,$fields) {
		$term = get_term_by('name', 'Custom Development', 'checkboxes_taxonomy_slug');
		
		if(!$term){
			return;
		}
		
		$tax_query = array_filter( (array) $query->get( 'tax_query' ) );
		
		$tax_query[] = [
			'taxonomy' => 'checkboxes_taxonomy_slug',
			'terms' => $term->term_id,
		];
		
		$query->set( 'tax_query', $tax_query );
	}, 
	1000, 
	2
);

I’m not sure that the action’s hook is working.

I tried your code, putting a die(); right after the inner function declaration (before the first var assignment), and the page still rendered. I know my functions.php script is executing, as putting the die before the add_action works fine (as in killing the engine).

Are you sure that ‘hivepress/v1/models/request/search’ is correct? I know based on the docs it should be (and I was happy that your guidance matched what I had provided)

Please note that the Action: hivepress/v1/models/{model_name}/search | HivePress Hook Reference (it is hivepress/v1/models/request/search in your case) hook works only when the user has searched on the page (click the Search button in the search form or change sort condition or click Filter button in the filter form). Also, please check if you have correctly changed the taxonomy slug (the word checkboxes_taxonomy_slug in the code snippet). It is possible to get the taxonomy slug when you click Edit Options in the attribute and it is possible to find the taxonomy slug between ?taxonomy= and &post_type words in the address bar of your browser.

If you want to change the showing requests results when the user opens the page, please try to make changes with pre_get_posts | Hook | WordPress Developer Resources instead of hivepress/v1/models/request/search, but please note that it can require further customization. If you are not familiar with the code customization, then please consider hiring someone for custom work https://fvrr.co/32e7LvY

Thanks, knowing I had to use the pre_get_posts allowed me to get where I needed.

For anyone else who may come across this, when using pre_get_posts you can’t use any WP_Query within, I had to go straight SQL queries using $wpdb (though there’s probably a way to do this that I couldn’t figure out).

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.