Reputation: 720
I've got a blog that supplies content to multiple MailChimp newsletters via RSS. The first newsletter works fine, but the second I'm having issues with. The issue I have is that the second newsletter has "hidden" content. This content isn't meant for wide consumption, so it doesn't appear on the frontpage, but is accessible elsewhere on the site. The snafu with this is that not all of this content is hidden, just some of it, while other pieces of content for this newsletter could overlap with the first newsletter. This obviously makes excluding everything problematic, as they could be assigned multiple categories, some of which I wouldn't want hidden.
The issue I'm running into is that I have a way to exclude this content from the frontpage, but not from the main RSS feed. I'm using WP Hide Post for this, which allows me to exclude from feed, which in turn removes it from all feeds, including the ones that feed the newsletter. I'm currently using /feed?cat=XXX to reference these feeds. Is there a way to make it so category feeds still work, just the main /feed RSS would exclude it?
Upvotes: 0
Views: 3116
Reputation: 11
Thank you! This worked perfectly to exclude a category from the main rss feed but still include it in the category rss feed.
However, there was a small syntax error in the code that crashed my site when I inserted it into the functions.php. It was missing the opening '{' bracket for the if statement. Here is the corrected code:
add_action('pre_get_posts', 'exclude_category' );
function exclude_category( &$wp_query )
// Exclude from loop, archive and feed but not from category page/feed
{ if( is_home() || ( is_feed() && !is_category() ) || ( is_archive() && !is_category() )) { // Exclude from home, feed, but not from category page/feed
set_query_var('category__not_in', array(120)); // Exclude category with ID 120
}
}
And of course, remember to change the '120' to your category id.
Thank you again. This was so helpful!
Upvotes: 1
Reputation: 137
add_action('pre_get_posts', 'exclude_category' );
function exclude_category( &$wp_query )
// Exclude from loop, archive and feed but not from category page/feed
if( is_home() || ( is_feed() && !is_category() ) || ( is_archive() && !is_category() )) { // Exclude from home, feed, but not from category page/feed
set_query_var('category__not_in', array(120)); // Exclude category with ID 120
}
}
Upvotes: 0
Reputation: 720
I ended up having to rethink this problem from another angle. Instead of being able to exclude posts from the main feed, I had to create unique feeds for each of my categories. This made it so these feeds were independent of the main feed and wouldn't be affected by the WP Hide Posts plugin.
What I did was drop this file into my theme folder, then create blank pages with this template assigned to it. At the top I have it handling each page I created and assigning it to the corresponding category.
<?php
/*
Template Name: Custom feeds
*/
$numposts = 10; // number of posts in feed
if ( is_page( 'custom-feed-1' )) {
$posts = query_posts('showposts='.$numposts.'&cat=1');
} elseif ( is_page( 'custom-feed-2' )) {
$posts = query_posts('showposts='.$numposts.'&cat=2');
} elseif ( is_page( 'custom-feed-3' )) {
$posts = query_posts('showposts='.$numposts.'&cat=3');
}
$more = 1;
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title><?php bloginfo_rss('name'); wp_title_rss(); ?> - Article Feed</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>
<?php the_generator( 'rss2' ); ?>
<language>en-US</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'); ?>
<?php while( have_posts()) : the_post(); ?>
<item>
<title><?php the_title_rss(); ?></title>
<link><?php the_permalink_rss(); ?></link>
<comments><?php comments_link(); ?></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() ?>]]></content:encoded>
<?php else : ?>
<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
<?php endif; ?>
<?php endif; ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>
Also I don't wish to lay claim to this template, I found it online awhile back and modified it a bit, I just can't remember where I came across it.
Upvotes: 0
Reputation: 146191
I'm not sure if you want this. I guessed you want to exclude one or more categories from your rss
feed and to do it you can add this code snippet in your functions.php
function myFeedExcluder($query) {
if ($query->is_feed) {
$query->set('cat','-10'); // excludes category with id 10
} // for multiple you can use ‘-10,-20,-30′
return $query;
}
add_filter('pre_get_posts','myFeedExcluder');
Also you can use in the link directly like
<a href="<?php bloginfo('url'); ?>/feed?cat=-10&cat=-20">Entries (RSS)</a>
<a href="<?php bloginfo('url'); ?>/feed=rss2&cat=-10,-20">Entries (RSS)</a>
You can also check this plugin to exclude some posts from feed
but I didn't test it and here is another article about excluding categories from RSS Feed And More
.
Upvotes: 0