Ticket #10885: 10885.4.diff

File 10885.4.diff, 3.8 KB (added by ryan, 4 years ago)

Alternative that adds an output argument to get_post_types()

Line 
1Index: wp-includes/post.php
2===================================================================
3--- wp-includes/post.php        (revision 11997)
4+++ wp-includes/post.php        (working copy)
5@@ -7,7 +7,22 @@
6  * @since 1.5.0
7  */
8 
9+//
10+// Post Type Registration
11+//
12+
13 /**
14+ * Creates the initial post types when 'init' action is fired.
15+ */
16+function create_initial_post_types() {
17+       register_post_type( 'post', array('exclude_from_search' => false) );
18+       register_post_type( 'page', array('exclude_from_search' => false) );
19+       register_post_type( 'attachment', array('exclude_from_search' => false) );
20+       register_post_type( 'revision', array('exclude_from_search' => true) );
21+}
22+add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
23+
24+/**
25  * Retrieve attached file path based on attachment ID.
26  *
27  * You can optionally send it through the 'get_attached_file' filter, but by
28@@ -408,6 +423,80 @@
29 }
30 
31 /**
32+ * Get a list of all registered post type objects.
33+ *
34+ * @package WordPress
35+ * @subpackage Post
36+ * @since 2.9.0
37+ * @uses $wp_post_types
38+ * @see register_post_type
39+ * @see get_post_types
40+ *
41+ * @param array|string $args An array of key => value arguments to match against the post types.
42+ *  Only post types having attributes that match all arguments are returned.
43+ * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
44+ * @return array A list of post type names or objects
45+ */
46+function get_post_types( $args = array(), $output = 'names' ) {
47+       global $wp_post_types;
48+
49+       $do_names = false;
50+       if ( 'names' == $output )
51+               $do_names = true;
52+
53+       $post_types = array();
54+       foreach ( (array) $wp_post_types as $post_type ) {
55+               if ( empty($args) ) {
56+                       if ( $do_names )
57+                               $post_types[] = $post_type->name;
58+                       else
59+                               $post_types[] = $post_type;
60+               } elseif ( array_intersect((array) $post_type, $args) ) {
61+                       if ( $do_names )
62+                               $post_types[] = $post_type->name;
63+                       else
64+                               $post_types[] = $post_type;
65+               }
66+       }
67+
68+       return $post_types;
69+}
70+
71+/**
72+ * Register a post type. Do not use before init.
73+ *
74+ * A simple function for creating or modifying a post type based on the
75+ * parameters given. The function will accept an array (second optional
76+ * parameter), along with a string for the post type name.
77+ *
78+ *
79+ * Optional $args contents:
80+ *
81+ * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true.
82+ *
83+ * @package WordPress
84+ * @subpackage Post
85+ * @since 2.9.0
86+ * @uses $wp_post_types Inserts new post type object into the list
87+ *
88+ * @param string $post_type Name of the post type.
89+ * @param array|string $args See above description.
90+ */
91+function register_post_type($post_type, $args = array()) {
92+       global $wp_post_types;
93+
94+       if (!is_array($wp_post_types))
95+               $wp_post_types = array();
96+
97+       $defaults = array('exclude_from_search' => true);
98+       $args = wp_parse_args($args, $defaults);
99+
100+       $post_type = sanitize_user($post_type, true);
101+       $args['name'] = $post_type;
102+       $wp_post_types[$post_type] = (object) $args;
103+}
104+
105+/**
106  * Updates the post type for the post ID.
107  *
108  * The page or post cache will be cleaned for the post ID.
109Index: wp-includes/query.php
110===================================================================
111--- wp-includes/query.php       (revision 11997)
112+++ wp-includes/query.php       (working copy)
113@@ -2072,8 +2072,12 @@
114 
115                $post_type_cap = $post_type;
116 
117+               $exclude_post_types = '';
118+               foreach ( get_post_types( array('exclude_from_search' => true) ) as $_wp_post_type )
119+                       $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type != %s", $_wp_post_type);
120+
121                if ( 'any' == $post_type ) {
122-                       $where .= " AND $wpdb->posts.post_type != 'revision'";
123+                       $where .= $exclude_post_types;
124                } elseif ( ! empty( $post_type ) ) {
125                        $where .= " AND $wpdb->posts.post_type = '$post_type'";
126                } elseif ( $this->is_attachment ) {