WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/01/2010 11:12:26 PM (12 years ago)
Author:
ryan
Message:

Permalinks for custom post types. Props prettyboymp. see #9674

File:
1 edited

Legend:

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

    r12898 r12923  
    1717function create_initial_post_types() {
    1818    register_post_type( 'post', array(  'label' => __('Posts'),
     19                                        'publicly_queryable' => true,
    1920                                        'exclude_from_search' => false,
    2021                                        '_builtin' => true,
     
    2223                                        'capability_type' => 'post',
    2324                                        'hierarchical' => false,
     25                                        'rewrite' => false,
     26                                        'query_var' => false,
    2427                                        'supports' => array('post-thumbnails', 'excerpts', 'trackbacks', 'custom-fields', 'comments', 'revisions')
    2528                                    ) );
    2629
    2730    register_post_type( 'page', array(  'label' => __('Pages'),
     31                                        'publicly_queryable' => true,
    2832                                        'exclude_from_search' => false,
    2933                                        '_builtin' => true,
     
    3135                                        'capability_type' => 'page',
    3236                                        'hierarchical' => true,
     37                                        'rewrite' => false,
     38                                        'query_var' => false,
    3339                                        'supports' => array('post-thumbnails', 'page-attributes', 'custom-fields', 'comments', 'revisions')
    3440                                    ) );
     
    3945                                            '_edit_link' => 'media.php?attachment_id=%d',
    4046                                            'capability_type' => 'post',
    41                                             'hierarchical' => false
     47                                            'hierarchical' => false,
     48                                            'rewrite' => false,
     49                                            'query_var' => false,
    4250                                        ) );
    4351
     
    4755                                            '_edit_link' => 'revision.php?revision=%d',
    4856                                            'capability_type' => 'post',
    49                                             'hierarchical' => false
     57                                            'hierarchical' => false,
     58                                            'rewrite' => false,
     59                                            'query_var' => false,
    5060                                        ) );
    5161
     
    687697 * label - A descriptive name for the post type marked for translation. Defaults to $post_type.
    688698 * public - Whether posts of this type should be shown in the admin UI. Defaults to false.
    689  * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true.
     699 * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
     700 * publicly_queryable - Whether post_type queries can be performed from the front page.  Defaults to whatever public is set as.
    690701 * inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
    691702 * capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
     
    702713 */
    703714function register_post_type($post_type, $args = array()) {
    704     global $wp_post_types;
    705 
    706     if (!is_array($wp_post_types))
     715    global $wp_post_types, $wp_rewrite, $wp;
     716
     717    if ( !is_array($wp_post_types) )
    707718        $wp_post_types = array();
    708719
    709720    // Args prefixed with an underscore are reserved for internal use.
    710     $defaults = array('label' => false, 'exclude_from_search' => true, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, '_show' => false, 'supports' => array());
     721    $defaults = array('label' => false, 'publicly_queryable' => null, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, '_show' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array());
    711722    $args = wp_parse_args($args, $defaults);
    712723    $args = (object) $args;
     
    714725    $post_type = sanitize_user($post_type, true);
    715726    $args->name = $post_type;
     727
     728    // If not set, default to the setting for public.
     729    if ( null === $args->publicly_queryable )
     730        $args->publicly_queryable = $args->public;
     731
     732    // If not set, default to true if not public, false if public.
     733    if ( null === $args->exclude_from_search )
     734        $args->exclude_from_search = !$args->public;
    716735
    717736    if ( false === $args->label )
     
    734753        add_post_type_support($post_type, $args->supports);
    735754        unset($args->supports);
     755    }
     756
     757    if ( false !== $args->query_var && !empty($wp) ) {
     758        if ( true === $args->query_var )
     759            $args->query_var = $post_type;
     760        $args->query_var = sanitize_title_with_dashes($args->query_var);
     761        $wp->add_query_var($args->query_var);
     762    }
     763
     764    if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
     765        if ( !is_array($args->rewrite) )
     766            $args->rewrite = array();
     767        if ( !isset($args->rewrite['slug']) )
     768            $args->rewrite['slug'] = $post_type;
     769        if ( !isset($args->rewrite['with_front']) )
     770            $args->rewrite['with_front'] = true;
     771        $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
     772        $wp_rewrite->add_permastruct($post_type, "/{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front']);
    736773    }
    737774
Note: See TracChangeset for help on using the changeset viewer.