Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Limit site search to posts and pages

  • Hi,
    I am using the document library pro plugin by barn2. It uses “documents” that are similar to posts. When I do a search using the search icon it returns the documents on the page instead of the actual page. How can I limit the site search to show only posts and pages?

  • Hi there,

    you can use the WP pre_get_posts hook to change the posttypes returned in the search results using this PHP Snippet:

    
    function include_cpt_search( $query ) {
        if ( !is_admin() && $query->is_main_query() && $query->is_search ) {
    	$query->set( 'post_type', array( 'post', 'page' ) );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'include_cpt_search' );
    

    This line: $query->set( 'post_type', array( 'post', 'page' ) ); we set the post_type arg in the query to just include the post and page

  • Worked perfectly! Thanks!

  • Glad to be of help

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.