Skip to content

Instantly share code, notes, and snippets.

@shubhw12
Created October 12, 2020 11:37
Show Gist options
  • Save shubhw12/a93c2c2bda6d36bb070f7c6c8aeaa97b to your computer and use it in GitHub Desktop.
Save shubhw12/a93c2c2bda6d36bb070f7c6c8aeaa97b to your computer and use it in GitHub Desktop.
Show only pages in the search results
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
}
add_filter( 'astra_infinite_pagination_post_type', 'astra_infinite_pagination_post_type_func' );
function astra_infinite_pagination_post_type_func( $post_type ) {
$post_type = 'page';
return $post_type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment