• I have written a plugin to do some transformations on the RSS feeds. Now I want to remove posts tagged with a certain tag from the feeds. I can’t figure out which hook to attach a filter to in order to return null and tell WordPress not to display the post, or if I should even be using any filters.

    I want to do this in my plugin, not use somebody else’s in addition to my own, if possible. Any suggestions? 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    function eliminate_tag_from_feed() {
    global $query_string;
    if (is_feed()) {
    $vars=parse_str($query_string);
    $vars['tag__not_in']=array(123,456,789);
    query_posts($vars);
    }
    }
    add_action('init','eliminate_tag_from_feed');

    I think that will work. The numbers 123 and 456 and so on are the ID numbers of the tags to exclude.

    Thread Starter gmorehoudh

    (@gmorehoudh)

    Thanks so much, I will try it out and let you know how it works. 🙂

    Thread Starter gmorehoudh

    (@gmorehoudh)

    Incidentally, do you think this would be any more straightforward using a category rather than a tag?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Well, yes in that there exist plugins to do it for you for categories. 🙂

    But, really, no. The code would be much the same. Slight differences only.

    Thread Starter gmorehoudh

    (@gmorehoudh)

    Doesn’t seem to be working:

    function hide_noaff_posts_from_feed($post) {
    	global $query_string;
    	if(is_feed()) {
    		$vars = parse_str($query_string);
    		$vars['tag__not_in'] = array(29);	// 29 is the id of the 'noaff' tag in the terms table
    		query_posts($vars);
    	}
    }

    […]

    add_action('init','hide_noaff_posts_from_feed');

    The add_action() is wrapped in an if block that only executes if a certain GET param is set. I’ve verified that the code inside the if block is executing, but the test post tagged with noaff is still appearing in the feed.

    Thread Starter gmorehoudh

    (@gmorehoudh)

    Okay, based on my investigation, I found that is_feed() was not yet true at init. This post confirms:

    http://comox.textdrive.com/pipermail/wp-hackers/2008-January/017441.html

    However, the suggestion of using template_redirect in that post causes the feed URL to display rendered HTML content. I’m reading and not sure what hook to try.

    Thread Starter gmorehoudh

    (@gmorehoudh)

    loop_start works. Thanks all! Hope this thread can help someone.

    Thread Starter gmorehoudh

    (@gmorehoudh)

    I’ve actually noticed a problem — I am using the id of the ‘noaff’ tag in the wp_terms table, but that’s yielding incorrect results. It’s removing many posts that are not tagged with ‘noaff’. Perhaps I’m using the id from the wrong table?

    Thread Starter gmorehoudh

    (@gmorehoudh)

    Looked it up in the manage tags section and the id is correct, but it’s removing many posts from the feed that are NOT tagged with the particular tag. I’d appreciate any help, I’ve been trying to look at the WordPress code itself but I’m not getting anywhere.

    I’m using a filter on pre_get_posts and it works for me.

    So:

    function filter_feed_posts($query) {
    if($query->is_feed) {
     //remove the tags
    }
    }
    add_filter('pre_get_posts','filter_feed_posts');

    Sorry, full function here:

    function filter_feed_posts($query) {
    if($query->is_feed) {
    //array of tags to exclude, by tag number
    $exclude_tags = array(1,2,3);
    $query->set('tag__not_in',$exclude_tags);
    }
    add_filter('pre_get_posts','filter_feed_posts');

    Thread Starter gmorehoudh

    (@gmorehoudh)

    This problem was actually caused by a bug in MySQL. Upgrading our servers to the latest version fixed it.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘remove tagged posts from rss feed’ is closed to new replies.