Restrict Search Results to Posts Only

No results found. Try again with different words?

Search must be at least 3 characters.

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
}

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.

Was this article helpful?
Did not find a solution? We are here to help you succeed.

Related Docs

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