<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[13774] trunk/wp-includes:
Add hierarchical support for custom post_types in Rewrite Rules & Querying
.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13774">13774</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2010-03-20 02:23:52 +0000 (Sat, 20 Mar 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Add hierarchical support for custom post_types in Rewrite Rules & Querying. See <a href="http://trac.wordpress.org/ticket/12643">#12643</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludeslinktemplatephp">trunk/wp-includes/link-template.php</a></li>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
<li><a href="#trunkwpincludesqueryphp">trunk/wp-includes/query.php</a></li>
<li><a href="#trunkwpincludesrewritephp">trunk/wp-includes/rewrite.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludeslinktemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/link-template.php (13773 => 13774)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/link-template.php        2010-03-20 02:05:32 UTC (rev 13773)
+++ trunk/wp-includes/link-template.php        2010-03-20 02:23:52 UTC (rev 13774)
</span><span class="lines">@@ -187,11 +187,16 @@
</span><span class="cx">
</span><span class="cx">         $draft_or_pending = 'draft' == $post->post_status || 'pending' == $post->post_status;
</span><span class="cx">
</span><ins>+        $post_type = get_post_type_object($post->post_type);
+
</ins><span class="cx">         if ( !empty($post_link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
</span><del>-                $post_link = ( $leavename ) ? $post_link : str_replace("%$post->post_type%", $slug, $post_link);
</del><ins>+                if ( ! $leavename ) {
+                        if ( $post_type->hierarchical )
+                                $slug = get_page_uri($id);
+                        $post_link = str_replace("%$post->post_type%", $slug, $post_link);
+                }
</ins><span class="cx">                 $post_link = home_url( user_trailingslashit($post_link) );
</span><span class="cx">         } else {
</span><del>-                $post_type = get_post_type_object($post->post_type);
</del><span class="cx">                 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
</span><span class="cx">                         $post_link = add_query_arg($post_type->query_var, $slug, '');
</span><span class="cx">                 else
</span><span class="lines">@@ -295,9 +300,8 @@
</span><span class="cx">
</span><span class="cx">         $link = false;
</span><span class="cx">
</span><del>-        if (! $id) {
</del><ins>+        if ( ! $id)
</ins><span class="cx">                 $id = (int) $post->ID;
</span><del>-        }
</del><span class="cx">
</span><span class="cx">         $object = get_post($id);
</span><span class="cx">         if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
</span><span class="lines">@@ -306,17 +310,18 @@
</span><span class="cx">                         $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
</span><span class="cx">                 else
</span><span class="cx">                         $parentlink = get_permalink( $object->post_parent );
</span><ins>+
</ins><span class="cx">                 if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
</span><span class="cx">                         $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
</span><span class="cx">                 else
</span><span class="cx">                         $name = $object->post_name;
</span><del>-                if (strpos($parentlink, '?') === false)
</del><ins>+
+                if ( strpos($parentlink, '?') === false )
</ins><span class="cx">                         $link = user_trailingslashit( trailingslashit($parentlink) . $name );
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        if (! $link ) {
</del><ins>+        if ( ! $link )
</ins><span class="cx">                 $link = trailingslashit(get_bloginfo('url')) . "?attachment_id=$id";
</span><del>-        }
</del><span class="cx">
</span><span class="cx">         return apply_filters('attachment_link', $link, $id);
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (13773 => 13774)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2010-03-20 02:05:32 UTC (rev 13773)
+++ trunk/wp-includes/post.php        2010-03-20 02:23:52 UTC (rev 13774)
</span><span class="lines">@@ -854,7 +854,10 @@
</span><span class="cx">                         $args->rewrite['slug'] = $post_type;
</span><span class="cx">                 if ( !isset($args->rewrite['with_front']) )
</span><span class="cx">                         $args->rewrite['with_front'] = true;
</span><del>-                $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
</del><ins>+                if ( $args->hierarchical )
+                        $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
+                else
+                        $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
</ins><span class="cx">                 $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -2749,7 +2752,7 @@
</span><span class="cx"> * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
</span><span class="cx"> * @return mixed Null when complete.
</span><span class="cx"> */
</span><del>-function get_page_by_path($page_path, $output = OBJECT) {
</del><ins>+function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
</ins><span class="cx">         global $wpdb;
</span><span class="cx">         $page_path = rawurlencode(urldecode($page_path));
</span><span class="cx">         $page_path = str_replace('%2F', '/', $page_path);
</span><span class="lines">@@ -2761,21 +2764,21 @@
</span><span class="cx">         foreach( (array) $page_paths as $pathdir)
</span><span class="cx">                 $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
</span><span class="cx">
</span><del>-        $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 ));
</del><ins>+        $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 ));
</ins><span class="cx">
</span><span class="cx">         if ( empty($pages) )
</span><span class="cx">                 return null;
</span><span class="cx">
</span><del>-        foreach ($pages as $page) {
</del><ins>+        foreach ( $pages as $page ) {
</ins><span class="cx">                 $path = '/' . $leaf_path;
</span><span class="cx">                 $curpage = $page;
</span><del>-                while ($curpage->post_parent != 0) {
-                        $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 ));
</del><ins>+                while ( $curpage->post_parent != 0 ) {
+                        $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 ));
</ins><span class="cx">                         $path = '/' . $curpage->post_name . $path;
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 if ( $path == $full_path )
</span><del>-                        return get_page($page->ID, $output);
</del><ins>+                        return get_page($page->ID, $output, $post_type);
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         return null;
</span></span></pre></div>
<a id="trunkwpincludesqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/query.php (13773 => 13774)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/query.php        2010-03-20 02:05:32 UTC (rev 13773)
+++ trunk/wp-includes/query.php        2010-03-20 02:23:52 UTC (rev 13774)
</span><span class="lines">@@ -1712,10 +1712,35 @@
</span><span class="cx">                 if ( $q['day'] )
</span><span class="cx">                         $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
</span><span class="cx">
</span><del>-                if ('' != $q['name']) {
</del><ins>+                if ( !empty($q['post_type']) && !empty($q[ $q['post_type'] ]) ) {
+                        $q[ $q['post_type'] ] = str_replace('%2F', '/', urlencode(urldecode($q[ $q['post_type'] ])));
+                        $post_type_object = get_post_type_object($q['post_type']);
+                        if ( ! $post_type_object->hierarchical || strpos($q[ $q['post_type'] ], '/') === false) {
+                                $q['name'] = $q[ $q['post_type'] ] = sanitize_title($q[ $q['post_type'] ]);
+                                $where .= " AND $wpdb->posts.post_name = '" . $q[ $q['post_type'] ] . "'";                        
+                        } else {
+                                // Hierarchical post type, need to look deeper to see if its an attachment or this post_type
+                                if ( isset($this->queried_object_id) ) {
+                                        $reqpage = $this->queried_object_id;
+                                } else {
+                                        $reqpage = get_page_by_path($q[ $q['post_type'] ], OBJECT, $q['post_type']);
+                                        if ( !empty($reqpage) )
+                                                $reqpage = $reqpage->ID;
+                                        else
+                                                $reqpage = 0;
+                                }
+                                $where .= " AND ($wpdb->posts.ID = '$reqpage')";
+                                $reqpage_obj = get_page($reqpage);
+                                if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
+                                        $this->is_attachment = true;
+                                        $q['attachment_id'] = $reqpage;
+                                        $post_type = $q['post_type'] = 'attachment';
+                                }
+                        }
+                } elseif ( '' != $q['name'] ) {
</ins><span class="cx">                         $q['name'] = sanitize_title($q['name']);
</span><span class="cx">                         $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
</span><del>-                } else if ('' != $q['pagename']) {
</del><ins>+                } elseif ( '' != $q['pagename'] ) {
</ins><span class="cx">                         if ( isset($this->queried_object_id) )
</span><span class="cx">                                 $reqpage = $this->queried_object_id;
</span><span class="cx">                         else {
</span><span class="lines">@@ -1727,7 +1752,7 @@
</span><span class="cx">                         }
</span><span class="cx">
</span><span class="cx">                         $page_for_posts = get_option('page_for_posts');
</span><del>-                        if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
</del><ins>+                        if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
</ins><span class="cx">                                 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
</span><span class="cx">                                 $page_paths = '/' . trim($q['pagename'], '/');
</span><span class="cx">                                 $q['pagename'] = sanitize_title(basename($page_paths));
</span><span class="lines">@@ -2802,4 +2827,4 @@
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-?>
</del><ins>+?>
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkwpincludesrewritephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/rewrite.php (13773 => 13774)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/rewrite.php        2010-03-20 02:05:32 UTC (rev 13773)
+++ trunk/wp-includes/rewrite.php        2010-03-20 02:23:52 UTC (rev 13774)
</span><span class="lines">@@ -1436,8 +1436,9 @@
</span><span class="cx">                                         // For custom post types, we need to add on endpoints as well.
</span><span class="cx">                                         foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
</span><span class="cx">                                                 if ( strpos($struct, "%$ptype%") !== false ) {
</span><ins>+                                                        $ptype = get_post_type_object($ptype);
</ins><span class="cx">                                                         $post = true;
</span><del>-                                                        $page = false;
</del><ins>+                                                        $page = $ptype->hierarchical; // This is for page style attachment url's
</ins><span class="cx">                                                         break;
</span><span class="cx">                                                 }
</span><span class="cx">                                         }
</span></span></pre>
</div>
</div>
</body>
</html>