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;
?>
post_type ) ) {
continue;
}
if ( true == $is_page ) {
do_action( 'astra_page_template_parts_content' );
} else {
do_action( 'astra_template_parts_content' );
}
?>
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.