Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active June 3, 2020 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/d2a21dc2dab8c449577600a6a5c16bfc to your computer and use it in GitHub Desktop.
Save jchristopher/d2a21dc2dab8c449577600a6a5c16bfc to your computer and use it in GitHub Desktop.
<?php
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment