WordPress.org

Ready to get started?Download WordPress

Forums

remove tagged posts from rss feed (13 posts)

  1. gmorehoudh
    Member
    Posted 5 years ago #

    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? :)

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    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.

  3. gmorehoudh
    Member
    Posted 5 years ago #

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

  4. gmorehoudh
    Member
    Posted 5 years ago #

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

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    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.

  6. gmorehoudh
    Member
    Posted 5 years ago #

    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.

  7. gmorehoudh
    Member
    Posted 5 years ago #

    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.

  8. gmorehoudh
    Member
    Posted 5 years ago #

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

  9. gmorehoudh
    Member
    Posted 5 years ago #

    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?

  10. gmorehoudh
    Member
    Posted 5 years ago #

    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.

  11. superann
    Member
    Posted 5 years ago #

    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');
  12. superann
    Member
    Posted 5 years ago #

    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');
  13. gmorehoudh
    Member
    Posted 5 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic