\SWP_Query
Table of Contents
\SWP_Query
an alternative to \SearchWP\Query
with a couple of important differences:
- The design and implementation is modeled after
WP_Query
. - As a result, regardless of
\SearchWP\Engine
configuration, results will be limited to\SearchWP\Source
s based onWP_Post
e.g. Posts, Pages, Media, and Custom Post Types.
Basic Usage
\SWP_Query
can be used just as you would (and in some cases swapped in for usage of) WP_Query
:
<?php | |
//@link https://searchwp.com/documentation/classes/swp_query/ | |
global $post; | |
$swp_query = new SWP_Query( [ | |
's' => 'coffee', // Search query. | |
'engine' => 'default', // Engine name. | |
] ); | |
if ( $swp_query->have_posts() ) { | |
while ( $swp_query->have_posts() ) : | |
$swp_query->the_post(); | |
?> | |
<div class="search-result"> | |
<h3><a href="<?php echo get_permalink(); ?>"> | |
<?php the_title(); ?> | |
</a></h3> | |
<?php the_excerpt(); ?> | |
</div> | |
<?php | |
endwhile; | |
wp_reset_postdata(); | |
} else { | |
?> | |
<p>No results found.</p> | |
<?php | |
} |
Note: There are some caveats in that \SWP_Query
does not have feature parity with WP_Query
. Please refer to this documentation which outlines what methods and properties are available to you.
Also note: Because \SWP_Query
was designed to be (mostly) interoperable with WP_Query
there may be times (e.g. when there is no s
(search query) parameter) you can swap WP_Query
back in to retrieve your results like so:
<?php | |
// @link https://searchwp.com/documentation/classes/swp_query/ | |
global $post; | |
$args = [ | |
's' => get_search_query(), | |
'tax_query' => [ [ | |
'taxonomy' => 'people', | |
'field' => 'slug', | |
'terms' => 'bob', | |
] ], | |
]; | |
// If a search query is present use SWP_Query | |
// else fall back to WP_Query | |
if ( ! empty( $args['s'] ) ) { | |
$swp_query = new SWP_Query( $args ); | |
} else { | |
$swp_query = new WP_Query( $args ); | |
} | |
// Loop through results. | |
if ( $swp_query->have_posts() ) { | |
while ( $swp_query->have_posts() ) : | |
$swp_query->the_post(); | |
?> | |
<div class="search-result"> | |
<h3><a href="<?php echo get_permalink(); ?>"> | |
<?php the_title(); ?> | |
</a></h3> | |
<?php the_excerpt(); ?> | |
</div> | |
<?php | |
endwhile; | |
wp_reset_postdata(); | |
} else { | |
?> | |
<p>No results found.</p> | |
<?php | |
} |
Arguments
When instantiating a new \SearchWP\Attribute
an array
of the following can be provided:
s
(string
)- Search query. (default:
''
) engine
(string
)\SearchWP\Engine
name. (default:'default'
)posts_per_page
(integer
)- Number of posts to return per page. (default:
get_option( 'posts_per_page' )
) load_posts
(boolean
)- Whether to return results as
WP_Post
s. (default:true
) fields
(string
)- Whether to return
"all"
or"ids"
. (default:"all"
) nopaging
(boolean
)- Whether to disable pagination and return all results. (default:
false
) page
(integer
)- Which page of results to return. (default:
null
) paged
(integer
)- Which page of results to return. (default:
1
) post__in
(integer[]
)- Limit search pool to these IDs (default:
[]
) post__not_in
(integer[]
)- Limit search pool to these IDs (default:
[]
) post_type
(string[]
)- Limit results to these Post Types (default:
[]
) post_status
(string[]
)- Limit results to these Post statii (default:
[]
) tax_query
([]
)- See
tax_query
(default:[]
) meta_query
([]
)- See
meta_query
(default:[]
) date_query
([]
)- See
date_query
(default:[]
) order
(string
)- Return results in
"ASC"
or"DESC"
order. (default:"DESC"
) orderby
(string
)- Return results sorted by
"relevance"
,"date"
, or"rand"
. (default:"relevance"
)
Methods
Note: There are magic getters and setters available for properties.
get_search_results()
- Update results set based on current properties.
get_posts()
- Returns the current results set.
Hooks
There are a number of hooks available to further modify the behavior of \SWP_Query
: