<!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>[15838] trunk/wp-includes:
  Fix custom post type feed when no regular posts present.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15838">15838</a></dd>
<dt>Author</dt> <dd>scribu</dd>
<dt>Date</dt> <dd>2010-10-18 21:06:49 +0000 (Mon, 18 Oct 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Fix custom post type feed when no regular posts present. Props aaroncampbell for initial patch. Fixes <a href="http://trac.wordpress.org/ticket/14922">#14922</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.php</a></li>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (15837 => 15838)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2010-10-18 20:50:35 UTC (rev 15837)
+++ trunk/wp-includes/functions.php        2010-10-18 21:06:49 UTC (rev 15838)
</span><span class="lines">@@ -280,6 +280,24 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Generates a unique key from an argument
+ * 
+ * @since 3.1
+ *
+ * @param mixed $arg
+ * @return string
+ */
+function wp_cache_key( $arg ) {
+        if ( is_scalar( $arg ) )
+                return md5( $arg );
+
+        $arg = (array) $arg;
+        sort( $arg );
+
+        return md5( serialize( $arg ) );
+}
+
+/**
</ins><span class="cx">  * Retrieve option value based on name of option.
</span><span class="cx">  *
</span><span class="cx">  * If the option does not exist or does not have a value, then the return value
</span></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (15837 => 15838)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2010-10-18 20:50:35 UTC (rev 15837)
+++ trunk/wp-includes/post.php        2010-10-18 21:06:49 UTC (rev 15838)
</span><span class="lines">@@ -3945,36 +3945,13 @@
</span><span class="cx">  *
</span><span class="cx">  * @since 0.71
</span><span class="cx">  *
</span><del>- * @uses $wpdb
- * @uses $blog_id
</del><span class="cx">  * @uses apply_filters() Calls 'get_lastpostdate' filter
</span><span class="cx">  *
</span><del>- * @global mixed $cache_lastpostdate Stores the last post date
- * @global mixed $pagenow The current page being viewed
- *
</del><span class="cx">  * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
</span><span class="cx">  * @return string The date of the last post.
</span><span class="cx">  */
</span><span class="cx"> function get_lastpostdate($timezone = 'server') {
</span><del>-        global $cache_lastpostdate, $wpdb, $blog_id;
-        $add_seconds_server = date('Z');
-        if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
-                switch(strtolower($timezone)) {
-                        case 'gmt':
-                                $lastpostdate = $wpdb-&gt;get_var(&quot;SELECT post_date_gmt FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1&quot;);
-                                break;
-                        case 'blog':
-                                $lastpostdate = $wpdb-&gt;get_var(&quot;SELECT post_date FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1&quot;);
-                                break;
-                        case 'server':
-                                $lastpostdate = $wpdb-&gt;get_var(&quot;SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1&quot;);
-                                break;
-                }
-                $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
-        } else {
-                $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
-        }
-        return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone );
</del><ins>+        return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -3985,45 +3962,62 @@
</span><span class="cx">  * 'gmt' is when the last post was modified in GMT time.
</span><span class="cx">  *
</span><span class="cx">  * @since 1.2.0
</span><del>- * @uses $wpdb
- * @uses $blog_id
</del><span class="cx">  * @uses apply_filters() Calls 'get_lastpostmodified' filter
</span><span class="cx">  *
</span><span class="cx">  * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
</span><span class="cx">  * @return string The date the post was last modified.
</span><span class="cx">  */
</span><span class="cx"> function get_lastpostmodified($timezone = 'server') {
</span><del>-        global $wpdb;
</del><ins>+        $lastpostmodified = _get_last_post_time( $timezone, 'modified' );
</ins><span class="cx"> 
</span><del>-        $add_seconds_server = date('Z');
-        $timezone = strtolower( $timezone );
-
-        $lastpostmodified = wp_cache_get( &quot;lastpostmodified:$timezone&quot;, 'timeinfo' );
-        if ( $lastpostmodified )
-                return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
-
-        switch ( strtolower($timezone) ) {
-                case 'gmt':
-                        $lastpostmodified = $wpdb-&gt;get_var(&quot;SELECT post_modified_gmt FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1&quot;);
-                        break;
-                case 'blog':
-                        $lastpostmodified = $wpdb-&gt;get_var(&quot;SELECT post_modified FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1&quot;);
-                        break;
-                case 'server':
-                        $lastpostmodified = $wpdb-&gt;get_var(&quot;SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1&quot;);
-                        break;
-        }
-
</del><span class="cx">         $lastpostdate = get_lastpostdate($timezone);
</span><span class="cx">         if ( $lastpostdate &gt; $lastpostmodified )
</span><span class="cx">                 $lastpostmodified = $lastpostdate;
</span><span class="cx"> 
</span><del>-        if ( $lastpostmodified )
-                wp_cache_set( &quot;lastpostmodified:$timezone&quot;, $lastpostmodified, 'timeinfo' );
-
</del><span class="cx">         return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+function _get_last_post_time( $timezone, $field ) {
+        global $wpdb, $blog_id;
+
+        if ( !in_array( $field, array( 'date', 'modified' ) ) )
+                return false;
+
+        $post_types = get_query_var('post_type');
+        if ( empty($post_types) )
+                $post_types = 'post';
+
+        $post_types = apply_filters( &quot;get_lastpost{$field}_post_types&quot;, (array) $post_types );
+
+        $key = &quot;lastpost{$field}:$blog_id:$timezone:&quot; . wp_cache_key( $post_types );
+
+        $date = wp_cache_get( $key, 'timeinfo' );
+
+        if ( !$date ) {
+                $add_seconds_server = date('Z');
+
+                array_walk( $post_types, array( &amp;$wpdb, 'escape_by_ref' ) );
+                $post_types = &quot;'&quot; . implode( &quot;', '&quot;, $post_types ) . &quot;'&quot;;
+
+                switch ( strtolower( $timezone ) ) {
+                        case 'gmt':
+                                $date = $wpdb-&gt;get_var(&quot;SELECT post_{$field}_gmt FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1&quot;);
+                                break;
+                        case 'blog':
+                                $date = $wpdb-&gt;get_var(&quot;SELECT post_{$field} FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1&quot;);
+                                break;
+                        case 'server':
+                                $date = $wpdb-&gt;get_var(&quot;SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1&quot;);
+                                break;
+                }
+
+                if ( $date )
+                        wp_cache_set( $key, $date, 'timeinfo' );
+        }
+
+        return $date;
+}
+
</ins><span class="cx"> /**
</span><span class="cx">  * Updates posts in cache.
</span><span class="cx">  *
</span></span></pre>
</div>
</div>

</body>
</html>