WordPress.org

Make WordPress Core

Changeset 11998


Ignore:
Timestamp:
10/06/2009 02:43:05 PM (9 years ago)
Author:
ryan
Message:

Post type registration. Exclude post types added via plugin from searches by default. Introduce register_post_type() and get_post_types(). fixes #10885

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r11978 r11998  
    77 * @since 1.5.0
    88 */
     9
     10//
     11// Post Type Registration
     12//
     13
     14/**
     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
    924
    1025/**
     
    406421
    407422    return false;
     423}
     424
     425/**
     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;
    408497}
    409498
  • trunk/wp-includes/query.php

    r11978 r11998  
    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'";
Note: See TracChangeset for help on using the changeset viewer.