<!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 &amp; 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 &amp; 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-&gt;post_status || 'pending' == $post-&gt;post_status;
</span><span class="cx"> 
</span><ins>+        $post_type = get_post_type_object($post-&gt;post_type);
+
</ins><span class="cx">         if ( !empty($post_link) &amp;&amp; ( ( isset($post-&gt;post_status) &amp;&amp; !$draft_or_pending ) || $sample ) ) {
</span><del>-                $post_link = ( $leavename ) ? $post_link : str_replace(&quot;%$post-&gt;post_type%&quot;, $slug, $post_link);
</del><ins>+                if ( ! $leavename ) {
+                        if ( $post_type-&gt;hierarchical )
+                                $slug = get_page_uri($id);
+                        $post_link = str_replace(&quot;%$post-&gt;post_type%&quot;, $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-&gt;post_type);
</del><span class="cx">                 if ( $post_type-&gt;query_var &amp;&amp; ( isset($post-&gt;post_status) &amp;&amp; !$draft_or_pending ) )
</span><span class="cx">                         $post_link = add_query_arg($post_type-&gt;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-&gt;ID;
</span><del>-        }
</del><span class="cx"> 
</span><span class="cx">         $object = get_post($id);
</span><span class="cx">         if ( $wp_rewrite-&gt;using_permalinks() &amp;&amp; ($object-&gt;post_parent &gt; 0) &amp;&amp; ($object-&gt;post_parent != $id) ) {
</span><span class="lines">@@ -306,17 +310,18 @@
</span><span class="cx">                         $parentlink = _get_page_link( $object-&gt;post_parent ); // Ignores page_on_front
</span><span class="cx">                 else
</span><span class="cx">                         $parentlink = get_permalink( $object-&gt;post_parent );
</span><ins>+
</ins><span class="cx">                 if ( is_numeric($object-&gt;post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
</span><span class="cx">                         $name = 'attachment/' . $object-&gt;post_name; // &lt;permalink&gt;/&lt;int&gt;/ is paged so we use the explicit attachment marker
</span><span class="cx">                 else
</span><span class="cx">                         $name = $object-&gt;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')) . &quot;?attachment_id=$id&quot;;
</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-&gt;rewrite['slug'] = $post_type;
</span><span class="cx">                 if ( !isset($args-&gt;rewrite['with_front']) )
</span><span class="cx">                         $args-&gt;rewrite['with_front'] = true;
</span><del>-                $wp_rewrite-&gt;add_rewrite_tag(&quot;%$post_type%&quot;, '([^/]+)', $args-&gt;query_var ? &quot;{$args-&gt;query_var}=&quot; : &quot;post_type=$post_type&amp;name=&quot;);
</del><ins>+                if ( $args-&gt;hierarchical )
+                        $wp_rewrite-&gt;add_rewrite_tag(&quot;%$post_type%&quot;, '(.+?)', $args-&gt;query_var ? &quot;{$args-&gt;query_var}=&quot; : &quot;post_type=$post_type&amp;name=&quot;);
+                else
+                        $wp_rewrite-&gt;add_rewrite_tag(&quot;%$post_type%&quot;, '([^/]+)', $args-&gt;query_var ? &quot;{$args-&gt;query_var}=&quot; : &quot;post_type=$post_type&amp;name=&quot;);
</ins><span class="cx">                 $wp_rewrite-&gt;add_permastruct($post_type, &quot;{$args-&gt;rewrite['slug']}/%$post_type%&quot;, $args-&gt;rewrite['with_front'], $args-&gt;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-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT ID, post_name, post_parent FROM $wpdb-&gt;posts WHERE post_name = %s AND (post_type = 'page' OR post_type = 'attachment')&quot;, $leaf_path ));
</del><ins>+        $pages = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT ID, post_name, post_parent FROM $wpdb-&gt;posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')&quot;, $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-&gt;post_parent != 0) {
-                        $curpage = $wpdb-&gt;get_row( $wpdb-&gt;prepare( &quot;SELECT ID, post_name, post_parent FROM $wpdb-&gt;posts WHERE ID = %d and post_type='page'&quot;, $curpage-&gt;post_parent ));
</del><ins>+                while ( $curpage-&gt;post_parent != 0 ) {
+                        $curpage = $wpdb-&gt;get_row( $wpdb-&gt;prepare( &quot;SELECT ID, post_name, post_parent FROM $wpdb-&gt;posts WHERE ID = %d and post_type = %s&quot;, $curpage-&gt;post_parent, $post_type ));
</ins><span class="cx">                         $path = '/' . $curpage-&gt;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-&gt;ID, $output);
</del><ins>+                        return get_page($page-&gt;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 .= &quot; AND DAYOFMONTH($wpdb-&gt;posts.post_date)='&quot; . $q['day'] . &quot;'&quot;;
</span><span class="cx"> 
</span><del>-                if ('' != $q['name']) {
</del><ins>+                if ( !empty($q['post_type']) &amp;&amp; !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-&gt;hierarchical || strpos($q[ $q['post_type'] ], '/') === false) { 
+                                $q['name'] = $q[ $q['post_type'] ] = sanitize_title($q[ $q['post_type'] ]);
+                                $where .= &quot; AND $wpdb-&gt;posts.post_name = '&quot; . $q[ $q['post_type'] ] . &quot;'&quot;;                        
+                        } else {
+                                // Hierarchical post type, need to look deeper to see if its an attachment or this post_type
+                                if ( isset($this-&gt;queried_object_id) ) {
+                                        $reqpage = $this-&gt;queried_object_id;
+                                } else {
+                                        $reqpage = get_page_by_path($q[ $q['post_type'] ], OBJECT, $q['post_type']);
+                                        if ( !empty($reqpage) )
+                                                $reqpage = $reqpage-&gt;ID;
+                                        else
+                                                $reqpage = 0;
+                                }
+                                $where .= &quot; AND ($wpdb-&gt;posts.ID = '$reqpage')&quot;;
+                                $reqpage_obj = get_page($reqpage);
+                                if ( is_object($reqpage_obj) &amp;&amp; 'attachment' == $reqpage_obj-&gt;post_type ) {
+                                        $this-&gt;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 .= &quot; AND $wpdb-&gt;posts.post_name = '&quot; . $q['name'] . &quot;'&quot;;
</span><del>-                } else if ('' != $q['pagename']) {
</del><ins>+                } elseif ( '' != $q['pagename'] ) {
</ins><span class="cx">                         if ( isset($this-&gt;queried_object_id) )
</span><span class="cx">                                 $reqpage = $this-&gt;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>-?&gt;
</del><ins>+?&gt;
</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' =&gt; false ) ) as $ptype ) {
</span><span class="cx">                                                 if ( strpos($struct, &quot;%$ptype%&quot;) !== false ) {
</span><ins>+                                                        $ptype = get_post_type_object($ptype);
</ins><span class="cx">                                                         $post = true;
</span><del>-                                                        $page = false;
</del><ins>+                                                        $page = $ptype-&gt;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>