Make WordPress Core

Ticket #10885: 10885.4.diff

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

Alternative that adds an output argument to get_post_types()

  • wp-includes/post.php

     
    77 * @since 1.5.0
    88 */
    99
     10//
     11// Post Type Registration
     12//
     13
    1014/**
     15 * Creates the initial post types when 'init' action is fired.
     16 */
     17function create_initial_post_types() {
     18        register_post_type( 'post', array('exclude_from_search' => false) );
     19        register_post_type( 'page', array('exclude_from_search' => false) );
     20        register_post_type( 'attachment', array('exclude_from_search' => false) );
     21        register_post_type( 'revision', array('exclude_from_search' => true) );
     22}
     23add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
     24
     25/**
    1126 * Retrieve attached file path based on attachment ID.
    1227 *
    1328 * You can optionally send it through the 'get_attached_file' filter, but by
     
    408423}
    409424
    410425/**
     426 * Get a list of all registered post type objects.
     427 *
     428 * @package WordPress
     429 * @subpackage Post
     430 * @since 2.9.0
     431 * @uses $wp_post_types
     432 * @see register_post_type
     433 * @see get_post_types
     434 *
     435 * @param array|string $args An array of key => value arguments to match against the post types.
     436 *  Only post types having attributes that match all arguments are returned.
     437 * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
     438 * @return array A list of post type names or objects
     439 */
     440function get_post_types( $args = array(), $output = 'names' ) {
     441        global $wp_post_types;
     442
     443        $do_names = false;
     444        if ( 'names' == $output )
     445                $do_names = true;
     446
     447        $post_types = array();
     448        foreach ( (array) $wp_post_types as $post_type ) {
     449                if ( empty($args) ) {
     450                        if ( $do_names )
     451                                $post_types[] = $post_type->name;
     452                        else
     453                                $post_types[] = $post_type;
     454                } elseif ( array_intersect((array) $post_type, $args) ) {
     455                        if ( $do_names )
     456                                $post_types[] = $post_type->name;
     457                        else
     458                                $post_types[] = $post_type;
     459                }
     460        }
     461
     462        return $post_types;
     463}
     464
     465/**
     466 * Register a post type. Do not use before init.
     467 *
     468 * A simple function for creating or modifying a post type based on the
     469 * parameters given. The function will accept an array (second optional
     470 * parameter), along with a string for the post type name.
     471 *
     472 *
     473 * Optional $args contents:
     474 *
     475 * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true.
     476 *
     477 * @package WordPress
     478 * @subpackage Post
     479 * @since 2.9.0
     480 * @uses $wp_post_types Inserts new post type object into the list
     481 *
     482 * @param string $post_type Name of the post type.
     483 * @param array|string $args See above description.
     484 */
     485function register_post_type($post_type, $args = array()) {
     486        global $wp_post_types;
     487
     488        if (!is_array($wp_post_types))
     489                $wp_post_types = array();
     490
     491        $defaults = array('exclude_from_search' => true);
     492        $args = wp_parse_args($args, $defaults);
     493
     494        $post_type = sanitize_user($post_type, true);
     495        $args['name'] = $post_type;
     496        $wp_post_types[$post_type] = (object) $args;
     497}
     498
     499/**
    411500 * Updates the post type for the post ID.
    412501 *
    413502 * The page or post cache will be cleaned for the post ID.
  • wp-includes/query.php

     
    20722072
    20732073                $post_type_cap = $post_type;
    20742074
     2075                $exclude_post_types = '';
     2076                foreach ( get_post_types( array('exclude_from_search' => true) ) as $_wp_post_type )
     2077                        $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type != %s", $_wp_post_type);
     2078
    20752079                if ( 'any' == $post_type ) {
    2076                         $where .= " AND $wpdb->posts.post_type != 'revision'";
     2080                        $where .= $exclude_post_types;
    20772081                } elseif ( ! empty( $post_type ) ) {
    20782082                        $where .= " AND $wpdb->posts.post_type = '$post_type'";
    20792083                } elseif ( $this->is_attachment ) {