is_feed()WP 1.5.0

Is the query for a feed?

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

No Hooks.

Return

true|false. Whether the query is for a feed.

Usage

is_feed( $feeds );
$feeds(string|string[])
Feed type or array of feed types to check against.
Default: ''

Examples

0

#1 Changing the request for feeds

Suppose we have a custom field skip_rss_flag where we set it to true if we want to exclude the post from the RSS feed.

Then, using the conditional tag is_feed(), we can change the request for our needs. To do this, insert the following code into the theme functions.php file and specify the custom field skip_rss_flag with the value true (or any other non-empty value) for the post you want to exclude from RSS:

add_filter( 'posts_where', 'skip_post_from_rss' );

function skip_post_from_rss( $where ){
	global $wpdb;

	// not feed.
	if( ! is_feed() ){
		return $where;
	}

	$where .= " AND $wpdb->posts.ID NOT IN ( 
		SELECT distinct(post_id) from $wpdb->postmeta 
		where $wpdb->postmeta.meta_key = 'skip_rss_flag'
	) ";

	return $where;
}

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 1.5.0 Introduced.

is_feed() code WP 6.6.2

function is_feed( $feeds = '' ) {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_feed( $feeds );
}