0

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;
}
2
  • 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
    Commented May 28, 2011 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
    Commented May 31, 2011 at 20:45

1 Answer 1

0

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!

2
  • 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
    Commented May 27, 2011 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
    Commented May 31, 2011 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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