Display search result in my theme's search.php template

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Display search result in my theme's search.php template

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #12914
    username94838
    username94838
    Participant

    Your plugin works great for my small vacantion rental website however I would like to use my theme’s result page when I display the results. While the ajax version works as It should, all the filters work, when I choose to display the results on another page, the filters don’t work anymore, I get returned all the results, not the filtered ones. This is the search.php template I am using. I guess the search parameters are wrong. Can you tell me what’s wrong with it? .

    <?php get_header(); ?>
    <?php
    $s=get_search_query();
    $args = array(
                    's' =>$s
                );
        // The Query
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
            _e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>");
            while ( $the_query->have_posts() ) {
               $the_query->the_post();
                     ?>
                        <li>
                            <a>"><?php the_title(); ?></a>
                        </li>
                     <?php
            }
        }else{
    ?>
            <h2 style='font-weight:bold;color:#000'>Nothing Found</h2>
            <div class="alert alert-info">
              <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
            </div>
    <?php } ?>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    • This topic was modified 6 years, 12 months ago by Ernest Marcinko Ernest Marcinko. Reason: PHP code fix
    • This topic was modified 6 years, 12 months ago by Ernest Marcinko Ernest Marcinko.
    #12930
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I think there might be actually two possible causes:

    1. Make sure that the search override is enabled: https://i.imgur.com/viRLn6S.png
    In this case however I don’t think that is going to solve the issue by itself.

    2. The issue with the code is that there is a duplicate query executed, which cannot be overridden by ajax search pro, on this line:

    $the_query = new WP_Query( $args );

    This is not neccessary, as when the execution gets to search.php file, the search results are already parsed, no need to query again.
    Simply the have_posts() and the_posts() calls should be made, without the query object.

    I’ve made a correction to your code, but I have not tested it, please be careful. This is what it should look like:

    Best,
    Ernest Marcinko

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


    #12934
    username94838
    username94838
    Participant
    You cannot access this content.
    #12935
    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 4 posts - 1 through 4 (of 4 total)

The topic ‘Display search result in my theme's search.php template’ is closed to new replies.