is_feed()
Checks if a feed is being requested at the current moment.
No Hooks.
Returns
true|false
.
Usage
if( is_feed( $feeds ) ){ ... }
- $feeds(string)
- Type of the feed to check.
Examples
#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() is feed code WP 6.8.1
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 ); }