WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#10885 closed defect (bug) (fixed)

Custom post types show up in search

Reported by: ryan Owned by: ryan
Milestone: 2.9 Priority: normal
Severity: normal Version: 2.8.4
Component: Query Keywords:
Focuses: Cc:

Description (last modified by ryan)

Custom post types can show up in search results. Searches set the post_type to "any". "any" queries pull in all post types except "revision", which is special cased. We need a means for plugins to exclude their type from searches. Either they can set a custom post_status to avoid getting in the search query as a matter of policy or provide API for registering the type and excluding it more formally.

Attachments (4)

10885.diff (2.8 KB) - added by ryan 5 years ago.
10885.2.diff (3.4 KB) - added by ryan 5 years ago.
10885.3.diff (4.1 KB) - added by ryan 5 years ago.
10885.4.diff (3.8 KB) - added by ryan 5 years ago.
Alternative that adds an output argument to get_post_types()

Download all attachments as: .zip

Change History (14)

comment:1 @ryan5 years ago

  • Description modified (diff)

comment:2 @ryan5 years ago

Thinking we add register_post_type() and exclude all custom post types from search by default with ability to opt-in.

comment:3 @dd325 years ago

Thinking we add register_post_type() and exclude all custom post types from search by default with ability to opt-in.

That sounds the best to me, Just have the initial list array('post', 'page', 'attachment') and let us filter it.. or.. Just let us filter the list which should be excluded..

@ryan5 years ago

comment:4 @ryan5 years ago

I considered simply adding an array of post types to exclude that can be filtered, but since other situations have come up where having a list of registered post types was needed I decide to go ahead and register them like we do with taxonomies.

comment:5 @dd325 years ago

Sounds fair enough to me.

Could we add a few GET api's instead of using the globals directly? Much like the taxonomies..

get_post_types()

Could even simplify things a little for calling functions by doing an array_intersect, for example, This'd only return items matching...

get_post_types( array('exclude_from_search' => false) );

Would save other code blocks having to iterate over and check for conditionals themselves.. Just a thought :)

comment:6 @ryan5 years ago

Sounds good.

@ryan5 years ago

comment:7 @ryan5 years ago

Patch adds get_post_types(). Should it return a list of post type names or objects?

comment:8 @dd325 years ago

Patch adds get_post_types().

Looks good to me :)

Should it return a list of post type names or objects?

Originally i thought Objects, But, The function name really suggests it merely returns an array of matching post_types.. But thats just my 2c.

@ryan5 years ago

comment:9 @ryan5 years ago

get_post_type_objects() returns objects. get_post_types() returns names.

@ryan5 years ago

Alternative that adds an output argument to get_post_types()

comment:10 @ryan5 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [11998]) Post type registration. Exclude post types added via plugin from searches by default. Introduce register_post_type() and get_post_types(). fixes #10885

Note: See TracTickets for help on using tickets.