Custom post types – RSS lastBuildDate issue

I’m using CMS Press for custom post types. The issue is the feed doesn’t validate because lastBuildDate is blank. I am not using WordPress’s default posts for anything and that is causing the problem.

I did a test post using WordPress’s default post and voila lastBuildDate was filled in and the feed validated.

Read More

As soon as I deleted the post it removed the date in lastBuildDate and the feed didn’t validate.

I’m using the code below to add the custom post types in the main feed but this is an issue with the feeds created by the custom post types also.

Am I missing something to get the lastBuildDate to populate?

if ( ( is_front_page() && false == $query->query_vars['suppress_filters'] ) || is_feed() ){
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        query_posts( array('post_type'=>array('post', 'games', 'entertainment', 'tech', 'podcasts'),'paged'=>$paged ) );
} 

I answered my own question below.

Related posts

Leave a Reply

3 comments

  1. This is the solution I found based off of @Rarst’s answer. Put this in the themes functions.php and it worked like a charm!

    add_filter('get_lastpostmodified', 'my_lastpostmodified');
    function my_lastpostmodified()
    {
        global $wpdb;
        $add_seconds_server = date('Z');
        return $wpdb->get_var("SELECT  DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER  BY post_modified_gmt DESC LIMIT 1");
    } 
    
  2. Use WordPress functionality

    The function get_lastpostmodified() is extensively documented. It says in the wordpress documentation that the function accepts a timezone and a post_type.

    Documentation

    From WordPress Trac: wp-includes/post.php1

    5355     * @param string $post_type Optional. The post type to check. Default 'any'.
    5356     * @return string The timestamp.
    5357     */
    5358    function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
    

    Examples:

    If you want to check for the last post modified of a certain post type (let’s say ‘GAMES’). You would call the function as follows:

    get_lastpostmodified('GMT', 'Games');
    

    Following with an example of my custom feed meant for a popular job site. Here I use a Custom Post Type (CPT) called ‘JOB’:

    // ...
    <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT', 'job'), false); ?> GMT</lastBuildDate><pre>
    // ...
    

    Sources:

    1 https://core.trac.wordpress.org/browser/tags/4.4.1/src/wp-includes/post.php

  3. lastBuildDate is getting filled with data returned by get_lastpostmodified() function. Which only checks for native post type, so if you have none – there is no date found.

    Filter get_lastpostmodified hook to return some other build date (in MySQL DATETIME format) instead of it.