Integrating ASP results with BuddyBoss Search Component's Results Template

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Integrating ASP results with BuddyBoss Search Component's Results Template

This topic contains 11 replies, has 2 voices, and was last updated by nickchomey18 nickchomey18 11 months, 3 weeks ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #35052
    nickchomey18
    nickchomey18
    Participant

    I love ASP’s templating for live search results, but it doesn’t appear to offer much for search page templating. My BuddyBoss plugin/theme has a comprehensive search functionality and results template with tabs for different post-type groups etc… The code is fairly complicated and calls upon 15 different “helper” files (one for each result type), which generates all of the html code per result, then aggregates that within post-type groupings etc… So, it seems desirable to leverage all of that.

    The following filter is provided for overriding BB’s query results, which expects a particular format.

    /*
    $results will have a structure like below */
    /*
    id | type | relevance | entry_date
    45 | groups | 1 | 2014-10-28 17:05:18
    40 | posts | 1 | 2014-10-26 13:52:06
    4 | groups | 0 | 2014-10-21 15:15:36
    */
    $results = apply_filters( 'bp_search_query_results', $results, $this );

    I’ve created the following function, but am not quite sure which ASP filter I should use to link the two, or even how to use it.

    asp_buddyboss_results ($results, $this) {
    
    return $results;
    }
    add_filter ('bp_search_query_results', 'asp_buddyboss_results', 10, 2);

    Do you have any tips on how I can achieve this?

    An alternative that I am considering is to extract the relevant code from the BB search component into my own php snippet(s) in my child theme, and then customize it as needed – such as changing the filter to use add_filter (‘asp_results’, ‘asp_buddyboss_results’, 10, 3); and also modifying the html-generating code.

    Does this seem like a better idea? It would be “safer” as any future changes in the BB code wouldn’t break anything. Any other particular suggestions?

    Also, can you please clarify asp_results vs asp_cpt_results, asp_buddyp_results, etc…? Does asp_results contain all results from the search instance, whereas the other ones only contain the relevant subset of results?

    Thanks!

    Attachments:
    You must be logged in to view attached files.
    #35070
    nickchomey18
    nickchomey18
    Participant

    After digging more into the BB search component search, it seems like it is pretty complicated with dozens of files and functions. I’m also not quite sure how intertwined it is with the rest of BB, so I am hesitant to make a duplicate/alternate version of it. Therefore, I think the first option would be ideal – try to use the BB search code as much as possible and just replace its query with ASP’s query and results.

    The filter mentioned in the previous reply only allows me to modify the results, and there doesn’t appear to be a way to prevent the (very inefficient, redundant) BB queries from being run to begin with.

    So, for now, I will just directly modify the BB Plugin code and then monitor future releases for changes to merge with these files.

    I have attached a file that contains the 3 relevant functions:

    1. bp_search_search_page_content() – gets called by a filter on the_content and checks if the current page is a search query and then calls the next function.

    2. prepare_search_page() – sets some args and then calls the main search function, generally just passing the search term along as the sole argument.

    3. do_search()
    – dynamically loops through about 15 different search helper files to generate sql queries which then get combined into $pre_search_query on line 182, and then the $results are generated on line 188.
    – It then generates groups and creates HTML for each group and result.

    I have confirmed that if I use “?s={phrase}&bp_search=1&view=content” as ASP’s Custom Redirect URL, it will initiate the first function above. So, it seems to me that all I need to do is delete/comment out between line 158 and 190 – the section that creates and runs the queries – and somehow pass the ASP results to this.

    So, two questions:

    1. Do you have a suggestion for another approach?
    2. If not, how can I pass the ASP results to the do_search () function?

    Thanks!

    Attachments:
    You must be logged in to view attached files.
    #35089
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    This might be a bit more complicated than this. Ajax Search Pro also does a few major checks before initiating the search override feature, so simply adding the query arguments may not do anything.

    My recommendation would be to somehow somewhere (probably into the do_search() function) initiate the Ajax Search Pro search query manually:

    $search_id = 1;
    $args = array(
    	"s" => $phrase,
    	"_ajax_search" => false,
    	"posts_per_page" => 20,
    	"page"  => isset( $_GET['paged'] ) ? $_GET['paged'] : 1
    );
    $asp_query = new ASP_Query($args, $search_id);
    $results = $asp_query->posts;

    Here you need to set the correct $search_id (is the shortcode id here), and the search $phrase
    Then the $results array will contain the results for the $phrase based on the search configuration.

    You could then either use this to print the results, or gather their IDs via a foreach loop and then pass them forward, depending on how the rest of the code works.

    Best,
    Ernest Marcinko

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


    #35093
    nickchomey18
    nickchomey18
    Participant

    Thanks very much. That seems like it will work just fine! I’ll experiment with it and follow up if there’s anything in particular that I need help with.

    #35100
    nickchomey18
    nickchomey18
    Participant

    Actually, I foresee various issues with this.

    If I initiate the new ASP_Query from within the BB code, that would require me to leave the search form as the BB search form, rather than change it to an ASP search form with a shortcode. This means that I would lose the ajax live search functionality, asp settings etc…

    So, I was hoping to replace the BB search form with an ASP shortcode, which would allow me to leverage all of ASP’s front-end functionality, and then pass the results to the BB search component, stripping out BB’s query and leaving the rest of the HTML generation intact.

    At best, it seems like if I use the code you’ve provided while also using an ASP search form, ASP will generate two (or perhaps even 3) queries, wouldn’t it? One for the ajax live search, perhaps another one when pressing enter/clicking the search button, and certainly one more when a new ASP_Query is initiated in the bb do_search function (presumably without the use of any front-end filter settings).

    Am I wrong about any of this? It seems like it would be ideal if the existing ASP search could use an ASP action or filter to pass its arguments/results to one of the 3 functions I attached in the previous comment. Or, perhaps the results are stored in one of the PHP Sueprglobal variables (e.g. $_POST), from where they can be grabbed by BB’s do_action?

    Perhaps another way to look at this is more generally – how does ASP typically pass results to our custom search.php html template file? Shouldn’t I be able to use that mechanism?

    • This reply was modified 11 months, 4 weeks ago by nickchomey18 nickchomey18.
    #35116
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I believe the buddypress results page is completely different from the default search results page – and in that case ajax search pro does not trigger whatsoever. So if there is a WordPress results page on the yoursite.com/?s=phrase URI and a completely different one for BuddyPress, then ajax search pro will do nothing there, no override code triggers.

    Best,
    Ernest Marcinko

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


    #35127
    nickchomey18
    nickchomey18
    Participant

    I don’t know anything about BuddyPress search since I use BuddyBoss, but as I detailed above

    I have confirmed that if I use “?s={phrase}&bp_search=1&view=content” as ASP’s Custom Redirect URL, it will initiate the [BuddyBoss search functions and html template generation]

    So, all I need to know is in what manner ASP passes its results to the search.php page, so that I can grab them from within the BuddyBoss Search functions and bypass the BuddyBoss search query. Can you please help me with this?

    #35132
    nickchomey18
    nickchomey18
    Participant

    I dug into the code a bit more and it appears that ASP just passes the results to WP_Query’s posts variable, which is then used by the Loop. But it appears to be cleared/inaccessible by the time the BuddyBoss code is initiated based on the URL parameters mentioned above.

    So, I have made a couple different modifications directly in the ASP code that allow me to access the search results elsewhere. I added a global variable to the end of wp-content\plugins\ajax-search-pro\includes\classes\search\class-asp-query.php that is equal to the returned array, $results. This can then be retrieved from within the BB search code. I also put this array in $GLOBAL[‘variable’], which seems to offer the same ability to be retrieved.

    This is obviously not a sustainable solution, given that it won’t persist with updates to ASP. So, it would be ideal if I could create this global variable via a do_action hook.

    In class-asp-search.php, there is a hook “do_action(‘asp_after_search’, $s, $results, $id);” but this appears to only be accessible by the live search. Or, at the very least, it is not accessible by the class-asp-searchoverride.php that is being used in this case.

    So, I am hoping you could perhaps do one of two things:

    1. move the ‘asp_after_search’ hook to a place (class-asp-query?) where it would be accessible to all search methods. After all, this is where ‘asp_before_search’, and 8 other after_contenttype_results hooks are located.

    2. create a new hook either at the end of class-asp-query.php or within class-asp-searchoverride.php

    I could then use one of these hooks to create a global variable of my own, which I could then use from my modified BuddyBoss search function.

    While you are at it, it would be greatly appreciated if you could add the SQL Query filters requested in this ticket: https://wp-dreams.com/forums/topic/can-you-please-add-additional-query_add_select-from-join-where-filters/

    If you have thoughts on the above or any alternative solutions for this problem, I would love to hear them.

    Thanks very much!

    #35135
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    1. & 2. I will make a copy of it to the non-ajax results handler too. You can also use the asp_results hook instead of that, I think it is better.

    Will answer your other query soon as well, but since these threads are somewhat beyond regular support queries, I need to take care of some other requests first – I hope you don’t mind.

    Best,
    Ernest Marcinko

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


    #35144
    nickchomey18
    nickchomey18
    Participant

    Hah, I didn’t even consider using a filter as an action hook and just returning an unmodified value! So, using ‘asp_results’ to generate a new global variable works just fine! That’s seems to be the solution to this whole thread – there’s no need to copy the hook to the override code (unless you think that is necessary anyway). I should be able to tinker away on my own on the BuddyBoss side of the code to get it to work now.

    As for the comment that these threads are beyond a regular support query – I hope you can clarify what you mean by that. I have no intention to overstep my bounds and take advantage of you. I have seen many tickets where you go as far as writing custom code for people, and all I’ve been essentially asking for in this thread is to know which hook I can use. Or, for the other thread, to add a few more hooks, which seems to me to be a very minor task/request which would allow me and anyone to have a lot more flexibility in helping ourselves.

    Anyway, for now I will add in my own SQL query hooks and hope that you’ll be able to incorporate them officially in your next release, at which point I can modify my code to align with your hooks.

    #35148
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Great! I will copy that anyways, if that hook exists it should work on both live and results page queries.

    By that I only meant, that I am more than happy to help and take requests as much as I can, but I have to prioritize queries with major issues first – such as front-end javascript conflicts, or reports of error messages, or where the plugin acts as completely broken. Then if I can, I recommend simple custom codes (I have a lot of them pre-written and collected over the years). I’m sorry if I phrased it weirldly, I only wanted to ask for a bit of patience, as there are a few issues I need to address first. It is very likely, that I will add those hooks as soon as possible, before the next release 🙂

    Best,
    Ernest Marcinko

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


    #35156
    nickchomey18
    nickchomey18
    Participant

    Thanks very much for the clarification – that makes complete sense. Anyway, I think I have what I need for now to figure this all out. I should be leaving you alone for a little while 😉

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

You must be logged in to reply to this topic.