Template Setup (Standard)
This section outlines how to display your search form and results using the standard method. At this point you should have already configured your search form according to the instructions.
In your template file, begin by initializing a WP_Advanced_Search object:
$my_search = new WP_Advanced_Search('my-form');
The object takes one parameter, which is a string containing the name of the form you used when you configured it in your functions.php file.
We can now display the search form anywhere in the template using the method the_form():
To display the search results on the page, we can set up a custom loop using the query() method:
$my_search = new WP_Advanced_Search('my-form'); $query = $my_search->query(); if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post(); the_title(); endwhile; endif;
Pagination links can be displayed by using the pagination() method:
$my_search->pagination(array('prev_text' => '«','next_text' => '»'));
The pagination method takes an optional array of arguments, and recognizes the same arguments as the core WordPress function paginate_links().
Displaying Results Count
You can also easily display how many results were returned using the results_range() method:
echo 'Displaying results ' . $my_search->results_range() . ' of ' . $wp_query->found_posts;
This method takes an optional array of arguments:
- pre – Content to display before the output
- marker – Content to use as the range marker (ie passing a hyphen “-” would show the range as “1-5″)
- post – Content to display after the output
$args = array('pre' => '', 'marker' => '-', 'post' => '');
Using the default arguments, the above code would output something like:
Displaying results 1-4 of 21