<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre, #msg p { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[11998] trunk/wp-includes: Post type registration.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/11998">11998</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2009-10-06 14:43:05 +0000 (Tue, 06 Oct 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Post type registration. Exclude post types added via plugin from searches by default. Introduce register_post_type() and get_post_types(). fixes <a href="http://trac.wordpress.org/ticket/10885">#10885</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
<li><a href="#trunkwpincludesqueryphp">trunk/wp-includes/query.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (11997 => 11998)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2009-10-05 01:49:19 UTC (rev 11997)
+++ trunk/wp-includes/post.php        2009-10-06 14:43:05 UTC (rev 11998)
</span><span class="lines">@@ -7,7 +7,22 @@
</span><span class="cx">  * @since 1.5.0
</span><span class="cx">  */
</span><span class="cx"> 
</span><ins>+//
+// Post Type Registration
+//
+
</ins><span class="cx"> /**
</span><ins>+ * Creates the initial post types when 'init' action is fired.
+ */
+function create_initial_post_types() {
+        register_post_type( 'post', array('exclude_from_search' =&gt; false) );
+        register_post_type( 'page', array('exclude_from_search' =&gt; false) );
+        register_post_type( 'attachment', array('exclude_from_search' =&gt; false) );
+        register_post_type( 'revision', array('exclude_from_search' =&gt; true) );
+}
+add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
+
+/**
</ins><span class="cx">  * Retrieve attached file path based on attachment ID.
</span><span class="cx">  *
</span><span class="cx">  * You can optionally send it through the 'get_attached_file' filter, but by
</span><span class="lines">@@ -408,6 +423,80 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Get a list of all registered post type objects.
+ *
+ * @package WordPress
+ * @subpackage Post
+ * @since 2.9.0
+ * @uses $wp_post_types
+ * @see register_post_type
+ * @see get_post_types
+ *
+ * @param array|string $args An array of key =&gt; value arguments to match against the post types.
+ *  Only post types having attributes that match all arguments are returned.
+ * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
+ * @return array A list of post type names or objects
+ */
+function get_post_types( $args = array(), $output = 'names' ) {
+        global $wp_post_types;
+
+        $do_names = false;
+        if ( 'names' == $output )
+                $do_names = true;
+
+        $post_types = array();
+        foreach ( (array) $wp_post_types as $post_type ) {
+                if ( empty($args) ) {
+                        if ( $do_names )
+                                $post_types[] = $post_type-&gt;name;
+                        else
+                                $post_types[] = $post_type;
+                } elseif ( array_intersect((array) $post_type, $args) ) {
+                        if ( $do_names )
+                                $post_types[] = $post_type-&gt;name;
+                        else
+                                $post_types[] = $post_type;
+                }
+        }
+
+        return $post_types;
+}
+
+/**
+ * Register a post type. Do not use before init.
+ *
+ * A simple function for creating or modifying a post type based on the
+ * parameters given. The function will accept an array (second optional
+ * parameter), along with a string for the post type name.
+ *
+ *
+ * Optional $args contents:
+ *
+ * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true.
+ * 
+ * @package WordPress
+ * @subpackage Post
+ * @since 2.9.0
+ * @uses $wp_post_types Inserts new post type object into the list
+ *
+ * @param string $post_type Name of the post type.
+ * @param array|string $args See above description.
+ */
+function register_post_type($post_type, $args = array()) {
+        global $wp_post_types;
+
+        if (!is_array($wp_post_types))
+                $wp_post_types = array();
+
+        $defaults = array('exclude_from_search' =&gt; true);
+        $args = wp_parse_args($args, $defaults);
+
+        $post_type = sanitize_user($post_type, true);
+        $args['name'] = $post_type;
+        $wp_post_types[$post_type] = (object) $args;
+}
+
+/**
</ins><span class="cx">  * Updates the post type for the post ID.
</span><span class="cx">  *
</span><span class="cx">  * The page or post cache will be cleaned for the post ID.
</span></span></pre></div>
<a id="trunkwpincludesqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/query.php (11997 => 11998)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/query.php        2009-10-05 01:49:19 UTC (rev 11997)
+++ trunk/wp-includes/query.php        2009-10-06 14:43:05 UTC (rev 11998)
</span><span class="lines">@@ -2072,8 +2072,12 @@
</span><span class="cx"> 
</span><span class="cx">                 $post_type_cap = $post_type;
</span><span class="cx"> 
</span><ins>+                $exclude_post_types = '';
+                foreach ( get_post_types( array('exclude_from_search' =&gt; true) ) as $_wp_post_type )
+                        $exclude_post_types .= $wpdb-&gt;prepare(&quot; AND $wpdb-&gt;posts.post_type != %s&quot;, $_wp_post_type);
+
</ins><span class="cx">                 if ( 'any' == $post_type ) {
</span><del>-                        $where .= &quot; AND $wpdb-&gt;posts.post_type != 'revision'&quot;;
</del><ins>+                        $where .= $exclude_post_types;
</ins><span class="cx">                 } elseif ( ! empty( $post_type ) ) {
</span><span class="cx">                         $where .= &quot; AND $wpdb-&gt;posts.post_type = '$post_type'&quot;;
</span><span class="cx">                 } elseif ( $this-&gt;is_attachment ) {
</span></span></pre>
</div>
</div>

</body>
</html>