<!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>[14072] trunk/wp-includes/query.php:
Alternative implementation of custom post_type query_var handling.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/14072">14072</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2010-04-11 13:51:24 +0000 (Sun, 11 Apr 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Alternative implementation of custom post_type query_var handling. fills name & pagename for custom post_types to reduce amount of code duplication. See <a href="http://trac.wordpress.org/ticket/12704">#12704</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesqueryphp">trunk/wp-includes/query.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/query.php (14071 => 14072)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/query.php        2010-04-11 11:10:54 UTC (rev 14071)
+++ trunk/wp-includes/query.php        2010-04-11 13:51:24 UTC (rev 14072)
</span><span class="lines">@@ -1712,57 +1712,49 @@
</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><ins>+                // If we've got a post_type AND its not "any" post_type.
</ins><span class="cx">                 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
</span><del>-                        $_pt = is_array($q['post_type']) ? $q['post_type'] : array($q['post_type']);
-                        foreach ( $_pt as $_post_type ) {
-                                if ( empty($q[ $_post_type ]) )
</del><ins>+                        foreach ( (array)$q['post_type'] as $_post_type ) {
+                                $ptype_obj = get_post_type_object($_post_type);
+                                if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
</ins><span class="cx">                                         continue;
</span><span class="cx">
</span><del>-                                $q[ $_post_type ] = str_replace('%2F', '/', urlencode(urldecode($q[ $_post_type ])));
-                                $post_type_object = get_post_type_object($_post_type);
-                                if ( ! $post_type_object->hierarchical || strpos($q[ $_post_type ], '/') === false) {
-                                        $q['name'] = $q[ $_post_type ] = sanitize_title($q[ $_post_type ]);
-                                        $_names[] = $q[ $_post_type ];
</del><ins>+                                if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false) {
+                                        // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
+                                        $q['name'] = $q[ $ptype_obj->query_var ];
</ins><span class="cx">                                 } else {
</span><del>-                                        // 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[ $_post_type ], OBJECT, $_post_type);
-                                                if ( !empty($reqpage) )
-                                                        $reqpage = $reqpage->ID;
-                                                else
-                                                        $reqpage = 0;
-                                        }
-                                        $_ids[] = $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';
-                                        }
</del><ins>+                                        // Hierarchical post_types will operate through the
+                                        $q['pagename'] = $q[ $ptype_obj->query_var ];
+                                        $q['name'] = '';
</ins><span class="cx">                                 }
</span><ins>+
+                                // Only one request for a slug is possible, this is why name & pagename are overwritten above.
+                                break;
</ins><span class="cx">                         } //end foreach
</span><ins>+                        unset($ptype_obj);
+                }
</ins><span class="cx">
</span><del>-                        if ( !empty($_names) || !empty($_ids) ) {
-                                $where .= ' AND (1=0';
-                                if ( !empty($_names) )
-                                        $where .= " OR $wpdb->posts.post_name IN('" . implode("', '", $_names) . "')";
-                                if ( !empty($_ids) ) {
-                                        $_ids = array_map('absint', $_ids);
-                                        $where .= " OR $wpdb->posts.ID IN(" . implode(',', $_ids) . ")";
-                                }
-                                $where .= ')';
-                        }
-                        unset($_ids, $_names, $_pt, $_post_type);
-                } elseif ( '' != $q['name'] ) {
</del><ins>+                if ( '' != $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><span class="cx">                 } elseif ( '' != $q['pagename'] ) {
</span><del>-                        if ( isset($this->queried_object_id) )
</del><ins>+                        if ( isset($this->queried_object_id) ) {
</ins><span class="cx">                                 $reqpage = $this->queried_object_id;
</span><del>-                        else {
-                                $reqpage = get_page_by_path($q['pagename']);
</del><ins>+                        } else {
+                                if ( 'page' != $q['post_type'] ) {
+                                        foreach ( (array)$q['post_type'] as $_post_type ) {
+                                                $ptype_obj = get_post_type_object($_post_type);
+                                                if ( !$ptype_obj || !$ptype_obj->hierarchical )
+                                                        continue;
+
+                                                $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
+                                                if ( $reqpage )
+                                                        break;
+                                        }
+                                        unset($ptype_obj);
+                                } else {
+                                        $reqpage = get_page_by_path($q['pagename']);
+                                }
</ins><span class="cx">                                 if ( !empty($reqpage) )
</span><span class="cx">                                         $reqpage = $reqpage->ID;
</span><span class="cx">                                 else
</span><span class="lines">@@ -1779,6 +1771,7 @@
</span><span class="cx">                                 $reqpage_obj = get_page($reqpage);
</span><span class="cx">                                 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
</span><span class="cx">                                         $this->is_attachment = true;
</span><ins>+                                        $post_type = $q['post_type'] = 'attachment';
</ins><span class="cx">                                         $this->is_page = true;
</span><span class="cx">                                         $q['attachment_id'] = $reqpage;
</span><span class="cx">                                 }
</span></span></pre>
</div>
</div>
</body>
</html>