Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Last active February 2, 2023 12:01
Show Gist options
  • Select an option

  • Save searchwpgists/6d24fdf7f2bc93f30f779c20cc2ab39d to your computer and use it in GitHub Desktop.

Select an option

Save searchwpgists/6d24fdf7f2bc93f30f779c20cc2ab39d to your computer and use it in GitHub Desktop.

Revisions

  1. searchwpgists revised this gist Feb 2, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion template.php
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@
    ];

    // If a search query is present use SWP_Query
    // else fall back to WP_Query
    // else fall back to WP_Query
    if ( ! empty( $args['s'] ) ) {
    $swp_query = new SWP_Query( $args );
    } else {
  2. searchwpgists created this gist Apr 12, 2022.
    42 changes: 42 additions & 0 deletions template.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    <?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
    }