Is it possible to add additional Views templates?

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Is it possible to add additional Views templates?

This topic contains 8 replies, has 2 voices, and was last updated by luyendao luyendao 6 years, 5 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #9533
    luyendao
    luyendao
    Participant

    Hi there – i have 3 search instances on this site i’m building, each instance searches for different post types and displays results in a different ways.

    One is close to vertical, and the other two are close to Polaroid except have different custom fields (ACF) to pull data from.

    Can you recommend the best way to do this? I’ve read up on your KB, and have copied the result template files into my own theme directory, that works.

    a) Can i add additional unique templates of my own?
    b) What’s the best way to modify these templates if they will include a lot of additional custom field meta data? Should i use hooks or just modify the template?

    Last question, where could i run an additional query after a search result has been returned? My use case is I have about 80 location post type with coordinate information, if a user finds location A – i want to run an additional query showing other locations within a 30 mile radius using the Google API.

    So the part i’m not sure about is just the first part, where to run the additional query.

    Thanks so much for the amazing plugin, I hope my questions aren’t out of the scope of your support.

    If you offer paid modifications, please let me know as well.

    Cheers,
    Luyen

    #9540
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Luyen,

    For the layouts I would recommend using filters instead of the templates, but it’s a personal prefenrence. I usually prefer filters for all cases.

    I would use the asp_results filter in your case, because it has all the information you might need. As an example code:

    As you can see, this filter passes the results array and the search ID, which is exactly what you need to decide what to do exactly for each search instance.

    —————-

    The additional query problem is a bit more complicated. I had to run a few tests to see if it’s even possible, as I have never thought of this possibility. Just recently I have implemented a search API, documentation is under construction yet. Long story short, there is a class called ASP_Query, which is very similar to the classic WP_Query class.

    I haven’t made a list of practical examples yet, but you can check all the possible arguments on this gist url: https://gist.github.com/anag0/60f0de34e933ad2e0ac580cc1050ff57

    That is the class you will need in order to make this work. Now, before we start, you will have to make 1 modification in a plugin file, otherwise it won’t work unfortunately.
    Open up the plugins\ajax-search-pro\includes\classes\ajax\class-asp-search.php file on your server and scroll to line 73, which should be this:

    $results = apply_filters('asp_only_non_keyword_results', $results);

    change that to:

    $results = apply_filters('asp_only_non_keyword_results', $results, $id, $s);

    Okay. Now the asp_only_non_keyword_results filter passes the search ID as well, which is mandatory, and useful as well. Don’t worry about this change, I’m going to include it in the upcoming release, so it won’t get lost after an update.

    Using this filter, it’s now possible to parse additional results and append them to the results list. An example function I used:

    This code executes another query with the search phrase “hello” and appends the results to the beginning of the current results list. Leave the ‘_ajax_search’ argument on true, it’s very important to get the correct results format in ajax context.

    You can of course then extend this basic example with the switch statement from the other example above, to do different queries for each search instance.

    Best,
    Ernest Marcinko

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


    #9544
    luyendao
    luyendao
    Participant

    Hi Ernest,

    Thank for you taking the time to answer in such depth, i prefer using filters as well although in some cases templates ends up being easier (unfortunately).

    Both of your researched examples are exactly what i need, once again thank you – your help and support are amazing.

    I look forward to leaving a detailed testimonial for your product.

    Luyen

    #9601
    luyendao
    luyendao
    Participant

    Hi Ernest,

    I’m running into some issue, i tried the filter and initially that worked, i could see some additional text strings being appended to each result.

    That was about 6 days ago when it worked, and today it stopped working. Of course my thought was did I do anything that could have changed it, i did make a bunch of settings changes to how things look but didn’t modify filter function you provided in the gist.

    I double checked the $id matches, and modifying the copies of results templates in my themes folder still works as does everything else.

    I have Query Monitor running, and don’t see anything being hooked in, can you recommend a way to debug this?

    Thanks!

    #9614
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    Usually what I do is that I change something radically.

    For starters, you should try to change the function to always return empty results, like so:

    function asp_append_more_results( $results, $id ) {
        return array();
    }

    Now, if you make a search query, it should always return no results whatsoever. If so, then the hook works. If not, then this hook is not executed.

    If the hook is not executed, then it’s most likely not included, or included in a wrong location. Try moving it to the main theme functions.php file, or to the child theme functions.php file – and see if it changes anything.

    Best,
    Ernest Marcinko

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


    #9687
    luyendao
    luyendao
    Participant

    Hi earnest,

    Thanks for your help – i can’t explain it. The hook when i tested it with the additional filter you provided returned empty, but for some reason on this one particular search instance it didn’t work. It would work on other instances I had – in the end i created a new instance and it worked fine, so I don’t know what the reason is but it’s OK.

    #10325
    luyendao
    luyendao
    Participant

    Hi Ernest,

    I feel bad asking for help all the time, but here goes:

    I added this into my theme:
    add_filter( ‘asp_only_non_keyword_results’, ‘asp_append_more_results’, 1, 2);

    function asp_append_more_results( $results, $id) {

    switch ($id) {
    case 11:

    $args = array(
    ‘s’ => ‘appleton’,
    ‘_ajax_search’ => true
    );
    $asp_q = new ASP_Query($args, $id);

    var_dump($asp_q);
    return array_merge($asp_q->posts, $results);

    }
    }

    11 – the search instance
    s – “appleton” a string I know will return a result

    If I search for something gibberish like “jkaldkj129390123” that would be considered a non-result and trigger the above hook right?

    I’ve tried variations of the search phrase such as an empty string, as well as keywords I know should match but it’s not showing up displaying “No Results” everytime. I cleared the search cache too, but not sure how to debug this.

    My final objective is if I query a match or non-match, i can run another query that will find nearby cities, intersect and merge that array with what I have in my DB. Of course that part is outside of the scope of this plugin, but that’s there I need to get to to be able to run that query based on a match or non-match criteria.

    I’m wondering if it’s better instead of using the ajax handler, I let the plugin override the wordpress ?s search functionality and run the logic in the search results “template”.

    What do you think?

    Thanks again for all your help – i bought another license for our production environment 🙂

    #10368
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    Sorry for the late response, I had a few business related things to take care of, and was only able to answer a few tickets quickly.

    If I search for something gibberish like “jkaldkj129390123” that would be considered a non-result and trigger the above hook right?

    Nope, actually it’s the opposite of that, this executes only when there are valid results.

    The best solution in this case would be to make an additional change to the plugin ajax handler to allow filtering the results, regardless they exist or not. So, first things first, open up the wp-content\plugins\ajax-search-pro\includes\classes\ajax\class-asp-search.php and scroll to line 74, which should be an empty line, and put this there:

    $results = apply_filters(‘asp_ajax_results’, $results, $id, $s, $sd);

    Now, this will allow manipulating with the ajax results, regardless of the outcome. Then, your code should translate to something like:

    Best,
    Ernest Marcinko

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


    #10378
    luyendao
    luyendao
    Participant

    Hi Ernest, thanks for your time – no worries about late responses I’m sure you get a lot of tickets and mine are probably the most annoying 🙂

    I have another question for you – the solution above of running an additional query is the ideal way to do it. As a temporary solution what i’ve been doing is for a CPT such as Appleton (a city post type), I added a custom text field to that post type which contains the names of cities within a 50k radius. So if someone searches for Neenah for example, it will show Appleton – a kind of pseudo proximity search.

    Of course this isn’t the right way to do it – I was just curious if the search engine can also search within an array, or serialized data? So let’s say instead of storing just a text string, i could store Neenah as a meta key, and a distance integer as a Meta Value.

    Then when someone searches for Neenah – i can order the results by distance.

    I’ve tried an ACF repeater but that didn’t work, apologies but i haven’t tried just regular custom fields out of the box, I will try that.

    I know it’s not the best way to do things – but just wondering what is indexable. Thanks, promise not to bug you after this 🙂

    • This reply was modified 6 years, 5 months ago by luyendao luyendao.
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.