WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 1 down vote accepted
<?php $search_query = get_search_query(); ?>

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

share|improve this answer
get_search_query returns the search criteria, not the results. I am looking to retrieve search results in my search.jsp. – Albin Joseph Jan 21 '12 at 14:38
global $posts; print_r($posts); You mean this? – Rob Vermeer Jan 21 '12 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? – Albin Joseph Jan 21 '12 at 14:56
Don't no for sure, but you can try: global $wp_query; print_r($wp_query); – Rob Vermeer Jan 21 '12 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). – Rob Vermeer Jan 21 '12 at 15:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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