WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/20/2010 02:23:52 AM (10 years ago)
Author:
dd32
Message:

Add hierarchical support for custom post_types in Rewrite Rules & Querying. See #12643

File:
1 edited

Legend:

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

    r13773 r13774  
    855855        if ( !isset($args->rewrite['with_front']) )
    856856            $args->rewrite['with_front'] = true;
    857         $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
     857        if ( $args->hierarchical )
     858            $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
     859        else
     860            $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
    858861        $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
    859862    }
     
    27502753 * @return mixed Null when complete.
    27512754 */
    2752 function get_page_by_path($page_path, $output = OBJECT) {
     2755function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
    27532756    global $wpdb;
    27542757    $page_path = rawurlencode(urldecode($page_path));
     
    27622765        $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
    27632766
    2764     $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = 'page' OR post_type = 'attachment')", $leaf_path ));
     2767    $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')", $leaf_path, $post_type ));
    27652768
    27662769    if ( empty($pages) )
    27672770        return null;
    27682771
    2769     foreach ($pages as $page) {
     2772    foreach ( $pages as $page ) {
    27702773        $path = '/' . $leaf_path;
    27712774        $curpage = $page;
    2772         while ($curpage->post_parent != 0) {
    2773             $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type='page'", $curpage->post_parent ));
     2775        while ( $curpage->post_parent != 0 ) {
     2776            $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type ));
    27742777            $path = '/' . $curpage->post_name . $path;
    27752778        }
    27762779
    27772780        if ( $path == $full_path )
    2778             return get_page($page->ID, $output);
     2781            return get_page($page->ID, $output, $post_type);
    27792782    }
    27802783
Note: See TracChangeset for help on using the changeset viewer.