Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#22031 closed enhancement (duplicate)

Conditional tags don't work within add_feed() callback function

Reported by: sanchothefat Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Feeds Keywords:
Focuses: Cc:


I was putting together a custom feed and wanted to pick a different template based on the context of where I was in the site. So for example I wanted one feed template on the home/front page and another for category pages.

Using add_feed() and browsing to that feed results in only is_feed() being true and not any of the other conditional tags.

I was wondering if a) there's a reason for that and b) if it's something we could fix.

I'm not entirely sure where to look though. Attempts at running $wp_query->parse_query_vars() to force the conditionals to be set were unsuccessful.

The following code is a quick demo:

add_feed( 'new_feed', 'feed_output' );

function feed_output() {
    if ( is_home() )
        echo 'home feed';
    if ( is_singular() )
        echo 'singular feed';
    if ( is_category() )
        echo 'category feed';

Nothing will be output on /feed/new_feed/ URLs even if on home, single or category page.

Change History (4)

#1 @sanchothefat
9 years ago

  • Cc robert@… added

#2 @ocean90
9 years ago

  • Keywords close added; dev-feedback removed

It doesn't make sense to return true for is_home or is_singular in a feed. It's a feed so is_feed returns true. You need to define your own query for the feed content.


add_feed( 'my-feed', 'my_feed_template' );

function feed_template() {
	load_template( ABSPATH . WPINC . '/feed-rss2.php' );

add_action( 'pre_get_posts', 'my_feed_content' );
function feed_content( $query ) {
	// Bail if $posts_query is not an object or of incorrect class
	if ( ! is_object( $query ) || ( 'WP_Query' != get_class( $query ) ) )

	// Bail if filters are suppressed on this query
	if ( $query->get( 'suppress_filters' ) )

	if ( ! $query->is_feed( 'my-feed' ) )

	$query->set( 'post_type', array( 'post', 'page' ) );

#3 @sanchothefat
9 years ago

@ocean90 I get what you're saying. Thing is though feeds change depending on context eg. /category/cat-name/feed/ is a feed for that category, so wouldn't it be useful to have a way of telling them apart from the main feed for example?

What about feed versions of the is_category() type conditionals eg. something like is_category_feed(), is_tax_feed() etc...?

My use case for this was generating alternate views of the same context for a WordPress to kindle feed.

Last edited 9 years ago by sanchothefat (previous) (diff)

#4 @ocean90
9 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #20899.

Note: See TracTickets for help on using tickets.