1

I'm trying to load the search results using Ajax. The results display correctly, but I can't seem to make get_search_query() work in order to make the search term appear before the results. I know I could do this with Javascript, but I'm really curious as to why the WordPress functions aren't working. I've also tried using $_GET['s'] and get_query_var('s'), neither of which works.

Please see my code below:

Functions.php

<?php add_action( 'wp_ajax_search_results', 'load_search_results' );
add_action( 'wp_ajax_nopriv_search_results', 'load_search_results' );

function load_search_results() {
    $query = $_POST['query'];
    $args = array(
        'post_status' => array( 'publish', 'inherit' ),
        's' => $query
    );
    $search = new WP_Query( $args );

    if ( $search->have_posts() ) :         
    ?>
        <h1><?php printf( __( 'Results for: %s', 'theme' ), get_search_query() ); ?></h1>
        <?php /* Start the Loop */ ?>
        <?php  while( $search->have_posts() ): $search->the_post();?>
            <?php get_template_part( 'content', get_post_type() ); ?>
        <?php endwhile;
    else :?>
        <p><?php _e( 'No results', 'theme' ); ?></p>
    <?php endif;    
    die();      
}?>


JS Code

    $(document).on('submit', '.search-form', function( event ) {
        event.preventDefault();
        var $input = $(this).find('input[name="s"]');
        var $sQuery = $input.val();
        $('#results').html('');
        $.ajax({
            url: ajaxPosts.ajaxurl,
            type: 'post',
            data: {
                action: 'search_results',
                query: $sQuery                  
            },
            success: function( html ) {
                $('#results').append( html );
            }
        });

    }); 
4
  • $_GET['s'] will not work for two reason. Firstly you make a POST request and not a GET request and secondly there is no variable with name s in your request.
    – Laxmana
    Commented Mar 27, 2017 at 15:44
  • Thank you for your reply. As I mentioned, these were secondary ideas. In any case, you're right - it works with POST :). Any idea as to why get_search_query() isn't working?
    – Bob
    Commented Mar 27, 2017 at 17:46
  • When executing an AJAX request through wp-admin/admin-ajax.php WordPress executes only the code registered to that request. So query vars etc are not initialized and that's why get_search_query() is not working. The flow of AJAX is different from the normal one.
    – Laxmana
    Commented Mar 27, 2017 at 19:07
  • Thank you very much @Laxmana. If you wish, you can post your comments as an answer so I can accept it as the correct answer. In any case, thanks!
    – Bob
    Commented Mar 27, 2017 at 19:51

1 Answer 1

0

$_GET['s'] will not work for two reason. Firstly you make a POST request and not a GET request and secondly there is no variable with name s in your request.

When executing an AJAX request through wp-admin/admin-ajax.php WordPress executes only the code (function) registered to that request. Also it loads WordPress Administration APIs and Ajax Handlers for WordPress Core. So query vars, templates etc are not initialized and that's why get_search_query() is not working. The flow of an AJAX request is different from the normal one and only the bare minimum is being loaded. You can see wp-admin/admin-ajax.php for more details of the flow.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.