Support » Fixing WordPress » Exclude Custom Post Types from Search Query Results

  • Resolved cmdshiftdesign

    (@cmdshiftdesign)


    I have a custom post type defined called “works”.

    I would like my search results to return EXCLUDING all pages and all posts with in the custom post type.

    This is not achievable using Plugins like Simple-Exclude or Search-Everything… and I would prefer to accomplish this WITHOUT adding a plugin.

Viewing 5 replies - 1 through 5 (of 5 total)
  • I might not be understanding you properly but on the off chance that I am you can just include exclude_from_search => true when you register your post_type.

    That worked! I had done that earlier and it wasn’t working.. but i suspect I had a typo i was overlooking :/

    CHEERS!

    Cool. Glad you got it working.

    sometimes we miss the obvious. I just spent half and hour trying to write a function to exclude a custom post type. Grrrrr….

    Or if any one wants to just search through selected post types simply add the function to function.php

    *note: ‘video-camera’, ‘software-solution’,’post’ are the post types which you want to search replace with you own.

    // search filter
    function fb_search_filter($query) {
    if ( !$query->is_admin && $query->is_search) {
    $query->set('post_type', array('video-camera', 'software-solution','post') ); // id of page or post
    }
    return $query;
    }
    add_filter( 'pre_get_posts', 'fb_search_filter' );

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Exclude Custom Post Types from Search Query Results’ is closed to new replies.