Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Without UI settings, filter search results by custom fields
- This topic has 2 replies, 2 voices, and was last updated 2 years, 6 months ago by
Ernest Marcinko.
-
AuthorPosts
-
November 25, 2023 at 12:12 am #46179
jla
ParticipantHi,
I’m probably missing something obvious, but I have the following use case:
– I have a ajax_search_pro shortcode which displays a searchbar
– I want to ensure that all post results returned by the search are filtered according to some filter on thecustom fieldsof the post
– I don’t want this to be an option from the user interface — the user should have no control over this
– My use case involves filtering results based on post metadata, according to some dynamically generated value.What I want to enable is functionality like this, in a template:
`
<div class=”projects-container”>
<?php echo do_shortcode(‘[wpdreams_ajaxsearchpro id=2]‘); ?>
</div>$current_user_id = get_current_user_id(); // Get the current user ID
$user_uuid = get_user_meta($current_user_id, ‘user-uuid’, true);// What I want is for search results to be filteres such that the the returned posts have a custom field
user-uuid= $user_uuid`I have similar use cases for other custom fields.
Thanks in advance for your help!
Jon
November 25, 2023 at 1:32 am #46180jla
ParticipantPlease disregard!
I found the answer in the documentation itself, apologies.
The solution was to write a plugin, as below, corresponding to my above use case:
`
add_filter(“asp_query_args”, “filter_search_by_user_uuid”, 10, 2);function filter_search_by_user_uuid($args, $search_id) {
// Replace ‘2’ with the ID of your specific search bar
$target_search_id = 2;if ($search_id != $target_search_id) {
// If this isn’t the target search bar, don’t modify the query
return $args;
}// Get the current user ID and retrieve their UUID
$current_user_id = get_current_user_id();
$user_uuid = get_user_meta($current_user_id, ‘user-uuid’, true);// Check if user UUID is available
if ($user_uuid) {
// Add a post meta filter to the query arguments
$args[‘post_meta_filter’] = array(
array(
‘key’ => ‘user-uuid’,
‘value’ => $user_uuid,
‘operator’ => ‘=’,
‘allow_missing’ => false
)
);
}return $args;
}`November 27, 2023 at 10:20 am #46192Ernest Marcinko
KeymasterYou cannot access this content.
-
AuthorPosts
- You must be logged in to reply to this topic.