Make WordPress Core

Opened 14 years ago

Closed 14 years ago

#14922 closed defect (bug) (fixed)

Invalid Custom RSS Feed for Custom Post Type without a regular Post

Reported by: actionnerd's profile actionnerd Owned by:
Milestone: 3.1 Priority: normal
Severity: normal Version: 3.0.1
Component: Feeds Keywords:
Focuses: Cc:


Adding a custom feed for a Custom Post Type via:

	//!shiny new rss feeds
	function create_eventsfeed() {
		load_template( TEMPLATEPATH . '/feeds/events.php');
	add_feed('events', 'create_eventsfeed');

(also tried using "add_action('do_feed_events', 'create_eventsfeed', 10, 1);")

The feed generated does have content according to but also returns 404 Page Not Found. Adding the feed to the feed reader results in a broken feed that it can't connect to.

However, if I add a single Post (having previously moved the "Hello World!" post to the trash), the generated feed works even though it has nothing to do with Posts as it only draws from a Custom Post Type. As far as I could figure, Custom RSS Feeds for Custom Post Types can only be generated if there are also regular Posts, even if they are mutually exclusive.

Here's the code I used for the RSS Feed template (taken directly from Wordpress — I know Future posts are technically bad for RSS Feeds but it didn't work without that either):

 * RSS2 Feed Template for displaying RSS2 Posts feed.
 * @package WordPress

header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
$more = 1;

echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>

<rss version="2.0"
	<?php do_action('rss2_ns'); ?>

	<title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
	<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
	<link><?php bloginfo_rss('url') ?></link>
	<description><?php bloginfo_rss("description") ?></description>
	<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
	<language><?php echo get_option('rss_language'); ?></language>
	<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
	<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
	<?php do_action('rss2_head'); ?>
		$eventsQuery = new WP_Query("post_type=events&post_status=publish,future");
		while ($eventsQuery->have_posts()) : $eventsQuery->the_post();
	?>	<item>
		<title><?php the_title_rss() ?></title>
		<link><?php the_permalink_rss() ?></link>
		<comments><?php comments_link_feed(); ?></comments>
		<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
		<dc:creator><?php the_author() ?></dc:creator>
		<?php the_category_rss() ?>

		<guid isPermaLink="false"><?php the_guid(); ?></guid>
<?php if (get_option('rss_use_excerpt')) : ?>
		<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php else : ?>
		<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
	<?php if ( strlen( $post->post_content ) > 0 ) : ?>
		<content:encoded><![CDATA[<?php the_content_feed('rss2') ?>]]></content:encoded>
	<?php else : ?>
		<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
	<?php endif; ?>
<?php endif; ?>
		<wfw:commentRss><?php echo esc_url( get_post_comments_feed_link(null, 'rss2') ); ?></wfw:commentRss>
		<slash:comments><?php echo get_comments_number(); ?></slash:comments>
<?php rss_enclosure(); ?>
	<?php do_action('rss2_item'); ?>
	<?php endwhile; ?>

Attachments (3)

14922.001.diff (3.6 KB) - added by aaroncampbell 14 years ago.
14922.002.diff (4.1 KB) - added by aaroncampbell 14 years ago.
14922.003.diff (5.8 KB) - added by scribu 14 years ago.

Download all attachments as: .zip

Change History (14)

#1 @dd32
14 years ago

  • Component changed from General to Feeds

#2 @aaroncampbell
14 years ago

I have a problem with something very similar. Basically, get_lastpostmodified() and get_lastpostdate() only check for post-type 'post'. Since I'm also including the post-type 'beer' in my main feed, the feed consistently returns a 304 (not modified) until a new 'post' is created. It wouldn't fix the above I don't think, but those two functions should have a filter to allow you to set what post types you want checked. I'll upload a patch for this issue in a minute or two.

#3 @aaroncampbell
14 years ago

  • Cc aaroncampbell added
  • Keywords has-patch dev-feedback added
  • Milestone changed from Awaiting Review to 3.1

I generated this patch based on what I had already done before finding this ticket. The patch attached simply adds two filters called get_lastpostdate_post_types and get_lastpostmodified_post_types respectively. This will fix my issue, allowing me to add the post type 'beer' to my feed and correctly consider that post type when checking the freshness of the feed.

I'm wondering if this should be modified to pass the current post type to the filters? If this is the preferred fix I can upload a new patch.

#4 @aaroncampbell
14 years ago

The second patch passes the array (from $wp_query->query_vars) to the filter. It feels a little kludgy, is there a better way to get the post types being queried?

Someone who's more familiar with this code should make sure doing this doesn't introduce any unexpected issues. It *does* seem to fix the RSS issue though.

14 years ago

#5 @scribu
14 years ago

  • Keywords dev-feedback removed


  • move duplicated code to _get_last_post_time()
  • introduce wp_cache_key()
  • cache values using wp_cache_*()

#6 @aaroncampbell
14 years ago

Thanks for the refresh scribu. It looks good. I'll try to test it tomorrow if I can.

#7 @scribu
14 years ago

  • Resolution set to fixed
  • Status changed from new to closed

#9 @scribu
14 years ago

Aftermath: #15543

#10 @scribu
14 years ago

  • Keywords has-patch removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

Need to fix the cache invalidation.

#11 @nacin
14 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.