Passing a custom callback function and sorting based on the return.

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Passing a custom callback function and sorting based on the return.

This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 year, 10 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #29661
    gabe80
    gabe80
    Participant

    Hi there.
    This isn’t really a specific issue…but after reading documentation and knowledgebase I can’t seem to figure out how to do what I would like to do.

    Somewhere in the process of doing a search…I would like to execute a custom call back function that compares the resulting posts to information that is specific to a user…and if that function returns true…put those posts on the top of the results (or otherwise style them so they stand out).

    The users have inventories of ingredients and there are posts that are recipes. I have a function that checks the recipe to the users inventory…and if they have the right ingredients…it means they can make the recipe.

    I already have the function that determines whether or not a person can make a recipe.

    I would like to be able to filter and/or sort the ASP results by whether or not a user can make the recipe.

    I have been through a lot of the documentation…but can’t seem to figure out how to accomplish this. If you can help me out or point me in the right direction I would appreciate it.

    Thanks you for your time.

    Gabe

    #29666
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Gabe,

    You are looking for the asp_results hook. That lets you make changes to the final results array before it is printed to the output.

    In you case, I would start of with something like this:

    add_filter( 'asp_results', 'asp_custom_results_order', 10, 1 );
    function asp_custom_results_order( $results ) {
    	$front = array(); // Results prioritized
    	$back = array();  // Rest of the results
    	
    	// Parse through each result item
    	foreach ($results as $k=>&$r) {
    		if ( isset($r->post_type) ) {
    			if ( your_function_to_check($r->id, get_current_user_id() ... ) ) {
    				$front[] = $r;
    			} else {
    				$back[] = $r;
    			}
    		} else {
    			$back[] = $r;
    		}
    	}
    
    	return array_merge($front, $back);
    }

    This code skeleton helps you separate the results into two parts – front and back. The $front array goes to the front of the list, the $back contains the rest of the results. Then you can go through all of the results, check whatever you need, and put the result into either the $front of the $back.

    Best,
    Ernest Marcinko

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


    #29725
    gabe80
    gabe80
    Participant

    For some strange reason I can’t get any ASP specific filters to fire. I can get core WP filters to fire.

    I tried both the code you provided and just copy/pasted some examples from the documentation and can’t seem to get them to do anything…even throw errors on intentional typos. I only have one search so the ID should be correct.

    The search works fine. I have the search filtering an elementor posts page as well…no problem. I just can’t seem to be able to grab on to that asp_results hook.

    #29730
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    May I take a look at this somewhere, or can you pelase share the code? I tried a few variations on our test servers, but it all worked okay.

    Best,
    Ernest Marcinko

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


    #29734
    gabe80
    gabe80
    Participant

    Thank you for your time Ernest. I got it sorted out. There was something upstream in my functions.php that was interfering with filters and short codes.

    The code example you provided is working great now.

    Thank you for your help and a great plugin!

    #29737
    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 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.