0

I'm trying to rewrite my search.php to search only for the post titles because I have a ton of posts published to my site (around 400k), which slows down the search speed to about 20s per search.

<?php get_header();
$search_term = $_GET['s'];
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts = new WP_Query( array(
        's'     => "$search_term",
        'ep_integrate'   => true,
        'post_type'      => 'post',
        'posts_per_page' => 21,           
        'orderby' => 'date',
         'order'   => 'DESC',
         'paged' => $paged,
    ) );
?>
<?php if ($posts->have_posts()) :
?>
<main>
<div class="container mt-5">
    <h4> Results for" <?php the_search_query(); ?></h4>
    <hr style="margin-top: 0;">
    <div class="row clear full-img"> ...
3
  • 1
    If you want to change the search query do not do a custom query in search.php. Use the pre_get_posts hook. Also, your code has ep_integrate, which suggests you’re using ElasticPress. If so, you can customise what fields are searched and how they’re weighted in the plugin’s settings. Commented May 30, 2022 at 15:27
  • Does this answer your question? how to limit search to post titles?
    – bosco
    Commented May 30, 2022 at 16:30
  • @JacobPeattie I removed all the other attributes and set it to title only, but it still is taking around 10-15 seconds. Commented Jun 1, 2022 at 12:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.