Without UI settings, filter search results by custom fields

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Without UI settings, filter search results by custom fields

This topic contains 2 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 6 months, 3 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #46179
    jla
    jla
    Participant

    Hi,

    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 the custom fields of 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

    #46180
    jla
    jla
    Participant

    Please 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;
    }
    
    
    #46192
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.