Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In WP 3.1, it appears that setting 'exclude_from_search' = TRUE for a custom post type excludes the post type not just from searches on the front-end, but also from any post query using 'post_type' = 'all'.

I can imagine plenty of scenarios where a plug-in developer would want to access ALL post types, even those excluded from front-end searches. Is this a bug, or is it just poorly documented?

Backstory: I have a custom post type using a custom "Post Expiration Date" field. In an attempt to automatically un-publish expired custom posts, I created a Content Expiration plugin which uses wp_schedule_event() to periodically unpublish posts where "Post Expiration Date" < Today. It worked great as long as I specified my custom post type, but it stopped working when I tried to make it more generic by setting 'post_type' = 'all' in the WP_Query arguments. Turns out the problem was the 'exclude_from_search' argument for the custom post type. Here's the pertinent code in the WP_Query class:

$exclude_post_types = '';
$in_search_post_types = get_post_types( array('exclude_from_search' => false) );
if ( ! empty( $in_search_post_types ) )
  $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");

if ( 'any' == $post_type ) {
  $where .= $exclude_post_types;
share|improve this question
are you able to share the code to un-publish custom posts? I've been hunting all over stack trying to find a solution to this question, and this thread is the closet I have found –  shawn May 28 '11 at 22:13
@shawn, I'd be happy to share the code. Do you have an existing question on StackExchange? I'd prefer to post it in an answer instead derailing in a comment. –  danblaker May 31 '11 at 20:45
add comment

1 Answer

This is hard to answer - you're asking us to look in the minds of the WP developers who wrote this code :-)

It is possible that they thought the only place where you need to query for posts of all types is when you are doing a search, and thus they named it that way.

It could be documented more clearly in the Codex. The Codex is a wiki, so feel free to add it yourself!

share|improve this answer
I'm still new to this WP development stuff, and when I didn't see anything about this in the Codex or core.trac.wordpress.org I figured it was intended behavior. I think I'll update the Codex and log a bug. Post types "excluded from search" can be queried via WP_Query if you specify the post type, so I think this is just an oversight. –  danblaker May 27 '11 at 17:11
Here's the ticket in WP Core trac: core.trac.wordpress.org/ticket/17592. It's already patched, so I'm off to do some testing. –  danblaker May 31 '11 at 20:47
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.