Lightning Rank - Private Blog Network

  • Home
  • Done 4 You PBN Service
  • Use My Premium PBN
  • Other Services
    • Amazon Associate Audit
    • Editorial Links
    • Buy Expired Domains
  • Contact
    • Affiliates

How To Limit WordPress Search Results to Specific Post Types

November 17, 2015 By Jon Gillham Leave a Comment

This short tutorial will show you how to limit WordPress search results to specific post types when creating WordPress sites that have multiple custom post types added. To acheive this functionality we will use the WordPress pre_get_posts filter to hook into the default WordPress search query.

Add this code below to your themes functions.php to limit your search query to the post types of Post and Page only.

function wpshock_search_filter( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array('post','page') );
    }
    return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');

The important part of the code above where you an add or remove post types is below.

array('post','page')

If for example you created a custom WordPress post type with the name of books and wanted this post type to be included in the search results you would alter the code like this example below.

array('post','page', 'books')

Filed Under: Wordpress Tutorials Tagged With: pre_get_posts, search results, wordpress filter, wordpress hack

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Services

[wp_cycle]

Recent Posts

  • What It takes to get to #1 in Google
  • Private Blog Network Building Service in 2016 – Is It Still Worth The Risk?
  • UpThemes Scale Back On Theme Options Within Their WordPress Themes
  • Create A WordPress Login Form Shortcode For Your Blog
  • WooThemes Back Online After Malicious Hack & Further DDoS Attack

Our Team

Jon Gillham

Father, Husband and mechanical engineer. Jon has the easy job he builds and tests systems that Kelley, Hmd and their teams execute!
Ahmed Ali

Ahmed is the man that makes sure everything runs smoothly. A true jack of all trades he makes sure each site is built to our high standards!

Quick Contact

Checking...

Ouch! There was a server error.
Retry »

Sending message...

Copyright © 2021 . Experts at Harnessing the Power of Expired Domains to Help You Outrank Your Competition