Wordpress - How to remove search.php page and search only posts?

Asked

Viewed 79 times

0

my wordpress search.php page is showing the posts and pages created, how do I show only the posts?

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

   <article id="post-<?php the_ID(); ?>" <?php post_class( 'cf' ); ?> role="article">

     <header class="entry-header article-header">
       CONSTRUÇÃO DO BLOCO
     </header>

   </article>

<?php endwhile; ?>

<?php else : ?>

2 answers

1

According to the documentation of the get_posts function:

get_posts

<?php $args = array('post_type' => 'post');
    $posts_array = get_posts( $args ); ?>
  • It came to work, but with it the pagination of the site to work.

  • True, @kaiquemix. Here in this post suggest doing the same thing as the top colleague, but with more details: https://stackoverflow.com/questions/24838864/how-do-i-get-pagination-to-work-for-get-posts-in-wordpress

1


Speak, my friend, all beauty?

It’s quite easy! Just add this code below in the functions.php of your theme, that pages will no longer be displayed in searches.

if (!is_admin()) {
function wpb_search_filter($query) {
   if ($query->is_search) {
      $query->set('post_type', 'post');
   }
return $query;
}
add_filter('pre_get_posts','wpb_search_filter');
}
  • Vlw that worked out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.