Restrict Search Results to Posts Only

Restrict Search Results to Posts Only

Astra provides a search option within a website along with a few styles.

When a user searches for any term (string), results are served from the entire website, including all your pages and posts. Though there is no out-of-the-box option to restrict the search to posts only, you can do it with a custom code.

This document will show you how to restrict the search to posts only. Further, you will also find additional code to exclude the WooCommerce products if you’re running an online store.

Restrict the Search To Posts

To limit your search to posts only, navigate to Dashboard > Appearance > Theme Editor. Here, you will need to add the following custom code to your Astra Child Theme functions.php file:

add_action( 'wp', 'astra_modify_search_loop', 99 );

/**
* Modify Search Loop.
*
* @return void
*/
function astra_modify_search_loop() {
    remove_action( 'astra_content_loop', array( Astra_Loop::get_instance(), 'loop_markup' ) );
    add_action( 'astra_content_loop', 'astra_redo_loop_markup' );
}

/**
* Redo loop to search only posts.
*
* @return void
*/
function astra_redo_loop_markup( $is_page = false ) {

    global $post;

    ?>
    <main id="main" class="site-main" role="main">

        <?php if ( have_posts() ) : ?>

            <?php do_action( 'astra_template_parts_content_top' ); ?>

            <?php
            while ( have_posts() ) :
                the_post();

                if ( is_search() && ( 'page' == $post->post_type ) ) {
                    continue;
                }

                if ( true == $is_page ) {
                    do_action( 'astra_page_template_parts_content' );
                } else {
                    do_action( 'astra_template_parts_content' );
                }

                ?>

            <?php endwhile; ?>

            <?php do_action( 'astra_template_parts_content_bottom' ); ?>

        <?php else : ?>

            <?php do_action( 'astra_template_parts_content_none' ); ?>

        <?php endif; ?>

    </main><!-- #main -->
    <?php
}

function astra_exclude_products_from_search( $query ) {
    if ( ! is_admin() && $query->is_search() ) {
        $query->set( 'post_type', 'post' );
    }
  }  

If you don’t have your Child Theme installed, please check this article on how to do it. Also, If you are not sure how to add this code, please check this article.

Using the Code With Woocommerce

In addition, if you have WooCommerce running on your website, your products will still be shown next to the posts.

Thus, to also exclude WooCommerce products from the search, you will need a modified version of the above-mentioned code.

Hence, you will need to replace this bolded part of the code:

if ( is_search() && ( 'page' == $post->post_type ) ) {
   continue;
}

…with this one:

if ( is_search() && ( 'page' == $post->post_type || 'product' == $post->post_type ) ) {
   continue;
}

Modifying the code this way will result in both pages and WooCommerce products being omitted from the search. Accordingly, only posts will be displayed as the search result.

That’s it. We hope this guide helped you.

If you face any issues during this procedure, please feel free to reach out to our support team. We’re always here to help.

Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

On this page

Take the First Step Toward Success Now.

You’re backed by an amazing team and a risk-free money-back guarantee!

Download is Just A Click Away!

Enter your email address and be the first to learn about updates and new features.

This field is required.

REWIND

YEAR IN REVIEW

Scroll to Top
Now choose your preferred
page builder addon
Choose your preferred page builder addon

Download is Just A Click Away!

Enter your email address and be the first to learn about updates and new features.

Download Free Astra Theme - Modal Popup Form