Showing the search results on the search page (i.e. going without the Ajax)

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Showing the search results on the search page (i.e. going without the Ajax)

This topic contains 11 replies, has 2 voices, and was last updated by Gabriel David Gabriel David 8 years, 6 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #2279
    Gabriel David
    Gabriel David
    Participant

    Hi,

    I would like to set it up so the search results from Ajax Search Pro are visible on the actual wordpress search results page. I am not afraid of customizing this in code if need be. What is the main function that returns search results and what file is it located in? Assuming the function is x($query) and it returns a json string (I know this probably isn’t the case), could I just call x($_GET[“s”]) on the search.php template? I would appreciate the results as raw and unformatted as possible.

    I await your response with baited breath.

    Gabe

    #2282
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    This is actually requested by some users right now. The problems are:

    1. The search uses POST method instead of GET, to prevent excessive external usage – I highly doubt I can change that as of now, or ever – it’s too much of a security risk.

    2. The data returned by the search is not in a compatible format with a standars “WP_Query” data. There are several reasons for this, manly related to search speed and compatibility and of course some data does not have the required fields to act like a post – which cannot be returned as a default search result.

    This issue is far more complicated, than a few modifications unfortunately. I have tried to solve this a few times as of now, but something always went wrong.

    The solution will involve creating a filter, which hooks to the default search filter, then includes the search files, sends the search data through somehow (selected checkboxes, etc), transforms the data to the ajax search pro, recieves the results, then converts the results to wordpress-compatible format and returns.

    Best,
    Ernest Marcinko

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


    #2285
    Gabriel David
    Gabriel David
    Participant

    1.) Why couldn’t you just do $_POST[“query”] = $_GET[“query”] in a custom fix? Also, how would it be a security risk to use GET parameters? Search results aren’t exactly high stakes. Technically POST would be a bit of a security risk too if we’re worried about data being stolen. If the concern is script injection, then I hope to god users already have something protecting them from this, or else your scripts usage of GET parameters is probably the last thing to be concerned about; that is unless there is some sort of vulnerability you know about that I don’t 🙂

    2.) I don’t believe it needs to be necessarily. Hell, if the pseudo-function the_results() returns a formatted html list, that would be sufficient for the purposes of this solution.

    Yeah, what I’m saying is I dont mind taking a crack at that; I just need to know how the search even shows the vertical drop down results. It doesn’t need to be anywhere near as customizable imo, as if you are foregoing using the default way a plugin works you need to understand you may have to forego some of the benefits.

    I mean, assuming the logic of your search is as follow:

    1.) Take post parameters and pass them to search function
    2.) Search function returns results
    3.) Format results

    It seems like the solution to this should be as simple as changing the location the search results get sent to and then formatting them differently. So all I really need to know is how do I programmatically get search results. I don’t mind coming up with my own way of formatting them (and as I mentioned, it doesnt necessarily need to be in standard WP_Query format)

    • This reply was modified 8 years, 7 months ago by Gabriel David Gabriel David.
    #2287
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    1. First of all this way the call can be accessed from outside your server, which poses a possible threat of excessive usage. GET is also limited in terms of parameters length. In some cases people like to exclude dozens of categories, posts etc.. which is all part of the fronted as well. So there is a chance of data loss.

    You can of course try if you want. Look in the search.php in the main directory. Lines 192-193:

            $_posts = new wpdreams_searchContent($params);
            $pageposts =  $_posts->search($s); 
    

    Basically that’s the heart of it. You can do a “var_dump($params);die();” to see what data is passed to the constructor. The class is located at includes/search_content.class.php. It extends the includes/search.class.php, so you will need that as well.
    The $pageposts variable will contain an array of the results. Pobably adapting this code is the best idea to start with.

    Good luck!

    Best,
    Ernest Marcinko

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


    #2294
    Gabriel David
    Gabriel David
    Participant

    So the next big question would be how can I construct $params in my own custom templates? Do most/all of the settings have a default value?

    #2295
    Gabriel David
    Gabriel David
    Participant

    Specifically, how do I call any of your functions from custom templates?

    #2296
    Gabriel David
    Gabriel David
    Participant

    This is currently the code I have:

    require_once(ABSPATH . ‘wp-content/plugins/ajax-search-pro/ajax_search.php’);
    $_POST[“aspp”] = $_GET[“s”];
    $_POST[“action”] = “ajaxsearchpro_search”;
    $_POST[“asid”] = 2;
    $_POST[“options”] = “qtranslate_lang=0&set_intitle=None&set_incontent=None&set_inexcerpt=None&set_inposts=None&set_inpages=None&set_inbpgroups=None&set_inbpusers=None&customset%5B%5D=event&customset%5B%5D=speakers&customset%5B%5D=location&categoryset%5B%5D=2&categoryset%5B%5D=3&categoryset%5B%5D=4&categoryset%5B%5D=1&categoryset%5B%5D=5&categoryset%5B%5D=6&categoryset%5B%5D=7”;

    echo “<h1>Search: “.$_POST[“aspp”];
    do_action(‘ASP_’.$_POST[“action”]);

    It returns -1

    Any ideas as to why?

    #2297
    Gabriel David
    Gabriel David
    Participant

    Nvm, ajax_searhc.php makes it die with a code of -1 if action isnt set before including

    #2298
    Gabriel David
    Gabriel David
    Participant

    So this code is a little weird, as something odd is going on with my search. It doesnt seem to actually be utilizing the search.php template, but that is something I personally need to fix but the code below needs some context:

    <?php if(isset($_GET[“s”])):
    require_once(ABSPATH . ‘wp-content/plugins/ajax-search-pro/search.php’);
    $_POST[“aspp”] = $_GET[“s”];
    $_POST[“action”] = “ajaxsearchpro_search”;
    $_POST[“asid”] = 2;
    $_POST[“options”] = “qtranslate_lang=0&set_intitle=None&set_incontent=None&set_inexcerpt=None&set_inposts=None&set_inpages=None&set_inbpgroups=None&set_inbpusers=None&customset%5B%5D=event&customset%5B%5D=speakers&customset%5B%5D=location&categoryset%5B%5D=2&categoryset%5B%5D=3&categoryset%5B%5D=4&categoryset%5B%5D=1&categoryset%5B%5D=5&categoryset%5B%5D=6&categoryset%5B%5D=7”;

    echo “<h1>Search: “.$_POST[“aspp”];
    echo ajaxsearchpro_search();
    //$_posts = new wpdreams_searchContent($params);
    //$pageposts = $_posts->search($s);
    else: ?>
    <?php get_template_part( ‘content’, ‘page’ ); ?>
    <?php endif; ?>

    Still need to format the returned JSON into html but
    GG WP

    #2299
    Gabriel David
    Gabriel David
    Participant

    Here is the finished working code; I had to go against good practice a bit and make a new function in search.php that is an exact close of ajaxsearchpro_search except for one caveat; it returns the results instead of printing them:

    <?php if(isset($_GET[“s”])):
    require_once(ABSPATH . ‘wp-content/plugins/ajax-search-pro/search.php’);
    $_POST[“aspp”] = $_GET[“s”];
    $_POST[“action”] = “ajaxsearchpro_search”;
    $_POST[“asid”] = 2;
    $_POST[“options”] = “qtranslate_lang=0&set_intitle=None&set_incontent=None&set_inexcerpt=None&set_inposts=None&set_inpages=None&set_inbpgroups=None&set_inbpusers=None&customset%5B%5D=event&customset%5B%5D=speakers&customset%5B%5D=location&categoryset%5B%5D=2&categoryset%5B%5D=3&categoryset%5B%5D=4&categoryset%5B%5D=1&categoryset%5B%5D=5&categoryset%5B%5D=6&categoryset%5B%5D=7”;

    echo “<h1>Search: “.$_POST[“aspp”].”</h1>”;
    $results = json_decode(ajaxsearchpro_search_two());

    $html = “”;
    foreach($results as $p):
    $html .= “<div class=’search-result-item’>”;
    $html .= “link.”‘><h3 style=’margin-top:5px;’>”.$p->title.”</h3>“;
    $html .= ““.$p->author.”“;
    $html .= “<p>”.stripslashes($p->content).”image.”‘ style=’float:left;’/></p>”;
    $html .= “link.”‘>View More“;
    $html .= “</div>”;
    endforeach;

    echo $html;

    //$_posts = new wpdreams_searchContent($params);
    //$pageposts = $_posts->search($s);
    else: ?>
    <?php get_template_part( ‘content’, ‘page’ ); ?>
    <?php endif; ?>

    Obviously the html needs to be styled but it works! In fact, I’m not even using the ajax search pro form for my search!!!! I really would be a proponent of you adding a feature like this to your plugin that allows for the default WP search to utilize your *AMAZING* searching algorithm, cause legit it is one of the best I’ve seen and/or used (the other one being relevanssi, though the free version doesnt search BP which is a pain)

    #2302
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    Nice job there. Yesterday I was thinking about this whole integration thing. I think I know what should I do – an API for the search. If I managed to create a documented API, then the integration to the default wordpress search would be so easy. To be honest the search code is still too messy. So many things had changed from previous versions and I had to maintain the backwards compatibility – that was a pain in the ass and a lots of unnecessary code. And of course my skills have also improved, so there is some nice code mixed with worse. (mostly on the backend)

    Still, an API would open up the plugin features for devs like you, and perhaps my future work would be much much easier in terms of integrating new features.

    Thanks for the kind words, and you indeed did a nice job integrating the search this fast. I don’t meet developers in this forum too often.

    Best,
    Ernest Marcinko

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


    #2383
    Gabriel David
    Gabriel David
    Participant

    Haha, I bet talking to another developer is a breath of fresh air. I feel the same way generally 🙂

    I think an API would be a great idea! The ability to use the functionality without being confined by the UI (which is great btw) would be awesome.

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

You must be logged in to reply to this topic.