Documentation

View the installation guide, browse the Knowledge Base, read the FAQ, find out about SearchWP's many hooks, get support

Note: SWP_Query was introduced in SearchWP 2.6

SWP_Query is a class available in SearchWP that aims to mimic the functionality of WP_Query to the best of it’s ability. The class is not feature complete when comparing it to WP_Query and likely never will be. It has its own set of arguments, some match WP_Query and others are specific to SearchWP. Those that match will in most cases behave exactly as their WP_Query counterparts but please review the documentation for each.

Usage

At the very least a parameter s must be passed along, it is the search to perform. By default the default search engine will be used. Much like WP_Query you can pass along as many or as few parameters as you’d like.

Basic Usage

<?php
/**
* Use SearchWP's SWP_Query to perform a search using the default engine
*/
$swp_query = new SWP_Query(
array(
's' => 'coffee', // search query
)
);
if ( ! empty( $swp_query->posts ) ) {
foreach( $swp_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="search-result">
<h3><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
</div>
<?php endforeach; wp_reset_postdata();
} else {
?><p>No results found.</p><?php
}
view raw gistfile1.php hosted with ❤ by GitHub

Use a Supplemental Search Engine

<?php
/**
* Use SearchWP's SWP_Query to perform a search using a supplemental engine
*/
$swp_query = new SWP_Query(
array(
's' => 'coffee', // search query
'engine' => 'my_engine', // search engine
)
);
if ( ! empty( $swp_query->posts ) ) {
foreach( $swp_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="search-result">
<h3><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
</div>
<?php endforeach; wp_reset_postdata();
} else {
?><p>No results found.</p><?php
}
view raw gistfile1.php hosted with ❤ by GitHub

Using all parameters

<?php
$swp_query = new SWP_Query(
array(
's' => 'coffee', // search query
'engine' => 'default', // engine to use
'post_type' => array(), // override enabled post types in engine config (SearchWP 2.7+)
'posts_per_page' => 10, // posts per page
'nopaging' => false, // disable paging?
'fields' => 'all', // set to 'ids' to return only post IDs (SearchWP 2.7+)
'page' => 1, // which page of results
'post__in' => array(), // limit potential results pool to array of IDs
'post__not_in' => array(), // explicity exclude IDs from search results
'tax_query' => array( // tax_query support
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
'meta_query' => array( // meta_query support
array(
'key' => 'age',
'value' => array( 3, 4 ),
'compare' => 'IN',
),
),
'date_query' => array( // date_query support
array(
'year' => 2015,
'month' => 6,
'day' => 1,
),
),
)
);
view raw gistfile1.php hosted with ❤ by GitHub

Methods

None at this point

Parameters

  • s — the search query
  • engine — the SearchWP engine to use (default: default)
  • posts_per_page — how many posts to return per page (default is the global posts per page defined in Settings > Reading in the WordPress admin)
  • nopaging — disable pagination and return all posts (default: false)
  • fields — whether to limit results to post IDs (default: all)
  • page — which page of results to return (default: 1)
  • post__in — array of post IDs to limit results to (default: empty array)
  • post__not_in — array of post IDs to exclude from results (default: empty array)
  • post_type (added in 2.7) — override the engine configuration with an array of post types
  • tax_query — see WP_Query‘s tax_query documentation
  • meta_query — see WP_Query‘s meta_query documentation
  • date_query — see WP_Query‘s date_query documentation
  • load_posts (deprecated in 2.7, use fields) — whether to limit results to post IDs (default: true)

Filters

See searchwp_swp_query_args

Fix Search on Your Site. No Coding Required!

Now you can utilize all of the content that's gone unrecognized by native WordPress search instantly with SearchWP

Get SearchWP