WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude Custom Post Types from Search Query Results (6 posts)

  1. cmdshiftdesign
    Member
    Posted 2 years ago #

    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.

  2. James Laws
    The WP Ninjas
    Posted 2 years ago #

    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.

  3. cmdshiftdesign
    Member
    Posted 2 years ago #

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

    CHEERS!

  4. James Laws
    The WP Ninjas
    Posted 2 years ago #

    Cool. Glad you got it working.

  5. Ken Dirschl
    Member
    Posted 2 years ago #

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

  6. shafraz
    Member
    Posted 1 year ago #

    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' );

Topic Closed

This topic has been closed to new replies.

About this Topic