Is there any way I can get the search results to a variable in search.php file? Now I have the following code in my search.php file.

<?php if (have_posts()) : ?>
    <h3 class="pagetitle"><?php _e( 'Search Results', 'buddypress' ) ?></h3>
        <?php while (have_posts()) : the_post(); ?>

I know the while loop uses some variable that contains the search results. Now I want to use this variable before the loop. I need this variable to do some processing before displaying the results.

1 Answer 1

<?php $search_query = get_search_query(); ?>

I think you're searching for this function: get_search_query()

  • get_search_query returns the search criteria, not the results. I am looking to retrieve search results in my search.jsp. Commented Jan 21, 2012 at 14:38
  • 1
    global $posts; print_r($posts); You mean this? Commented Jan 21, 2012 at 14:41
  • That seems to be close. But it has only 10 records in it. What if I need to get all the search results? Commented Jan 21, 2012 at 14:56
  • Don't no for sure, but you can try: global $wp_query; print_r($wp_query); Commented Jan 21, 2012 at 15:04
  • Hmm I think that's not it, you can get all the results by making a custom query with your search var and posts_per_page at -1 (infinite). Commented Jan 21, 2012 at 15:08

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.