Skip to content

Instantly share code, notes, and snippets.

@alextoul
Created May 28, 2024 14:32
Show Gist options
  • Save alextoul/58e8e0a6fe6a48f2077289becc6bcd97 to your computer and use it in GitHub Desktop.
Save alextoul/58e8e0a6fe6a48f2077289becc6bcd97 to your computer and use it in GitHub Desktop.
<?php
// Get the search query
$search_query = sanitize_text_field($_GET['s']);
// Create a new WP_Query object with the search parameters
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'orderby' => 'date',
's' => $search_query,
);
// Use Relevanssi to perform the search
$search_query = new WP_Query($args);
relevanssi_do_query($search_query);
// Check if there are any posts
if ($search_query->have_posts()) :
while ($search_query->have_posts()) : $search_query->the_post();
// Output the post content
the_title('<h2>', '</h2>');
the_excerpt();
endwhile;
else :
echo '<p>No posts found.</p>';
endif;
// Reset the global post data
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment