Only search WP posts


/ Published in: PHP
Save to your folder(s)

To exclude pages from wordpress search results include the following in your theme's functions.php


Copy this code and paste it in your HTML
  1. /*
  2.  *Only Search Posts
  3.  */
  4. function SearchFilter($query) {
  5. if ($query->is_search) {
  6. $query->set('post_type', 'post');
  7. }
  8. return $query;
  9. }
  10.  
  11. add_filter('pre_get_posts','SearchFilter');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.