• Resolved sjferwerda

    (@sjferwerda)


    First of all, thanks for the great plugin @ravanh.

    We need to exclude or strip/filter out certain keywords from $keywords before they are echoed on line 139 of feed-sitemap-news.php. I’ve looked through the available filters and it does not appear that there is one that will work. We considered using “the_title_xmlsitemap” but we need to target just the keywords. Would it be possible to add a keywords filter to the next release? I assume this would be easier than adding a field in the settings to exclude certain keywords.

    Our keywords are pulled in from post categories and there are certain category names that we want to exclude in the keywords. I know that you have the “Limit to posts in these post categories:” option, but that does help in this case because for example a post might be assigned to both the News and Main Carousel categories. We want to show posts from the News category but we don’t want “Main Carousel” to show up in the keywords. Hope that makes sense?

    Thanks!

    Steve

    • This topic was modified 8 years, 4 months ago by sjferwerda.
    • This topic was modified 8 years, 4 months ago by sjferwerda.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi Steve, the plugin uses WordPress own function get_the_terms() which in turn respects the filter “get_the_terms” https://developer.wordpress.org/reference/hooks/get_the_terms/

    You could make your filter conditional with an if ( is_feed() ) which would apply for all feed types including sitemaps.

    Would that be useful?

    Thread Starter sjferwerda

    (@sjferwerda)

    Awesome, thanks for the quick reply and suggestion. I’ll give it a shot and then follow up with our solution if we can get it to work.

    Thank you in advance for the follow-up. It will be useful for others, I’m sure πŸ™‚

    Thread Starter sjferwerda

    (@sjferwerda)

    Thanks @ravanh! I’m testing the following solution and it appears to be working. Not sure what the best way is to target the sitemap-news feed/page but for now I’m using if ( strpos( $_SERVER[‘REQUEST_URI’], ‘sitemap-news.xml’ ) !== false )

    
    			public function stripSitemapNewsPriorityTerms( $terms, $id, $taxonomy ) {
    
    				if ( strpos( $_SERVER['REQUEST_URI'], 'sitemap-news.xml' ) !== false ) {
    
    					$priortiyArr = ['priority', 'main-carousel', 'main-headline', 'carousel', 'headline', 'trending'];
    			    foreach ( $terms as $key => $term ) {
    						if ( in_array( $term->slug, $priortiyArr ) ) {
    							unset( $terms[$key] );
    						}
    			    }
    
    			}
    				return $terms;
    			}
    add_filter( 'get_the_terms', array( $this, 'stripSitemapNewsPriorityTerms' ), 10, 3 );
    

    I was thinking more complex by passing through the request filter first… something like this:

    
    function my_filter_terms( $terms ) {
        ...
    }
    
    function my_filter_request( $request ) {
        if ( isset($request['feed']) && strpos($request['feed'],'sitemap-news') === 0 ) {
            add_filter( 'get_the_terms', 'my_filter_terms' );
        }
    }
    
    add_filter('request', 'my_filter_request', 0 );
    

    But your solution is much simpler and should therefor be more efficient. As long as the $_SERVER[‘REQUEST_URI’] global is available which is said to not always be the case (on IIS servers as far as I heard).

    But if it works for you, it should remain working as long as you stay on the same server. Thanks for haring! πŸ™‚

    Thread Starter sjferwerda

    (@sjferwerda)

    Sounds good, we have control over our server environment, so I think we should be good checking against $_SERVER[‘REQUEST_URI’].

    Thanks again! πŸ™‚

    Thinking about it more, I’ll be adding a conditional to check against in the next release. Something like is_sitemap() or other…

    Thread Starter sjferwerda

    (@sjferwerda)

    I guess one might be able to use is_feed( $feeds ) or $wp_query->is_feed( $feeds ) where $feeds is the feed type ‘sitemap-news’. However, is_sitemap() would probably be more idiot proof.

    • This reply was modified 8 years, 4 months ago by sjferwerda.

    I’ve not tested this but my impression was that doing an is_feed( 'sitemap-news' ) that would (if sitemap-news has been registered as a feed URL) always return true independent of the current request. Only is_feed() without the $feeds parameter will validate for the current request…

    Correct me if I’m wrong though πŸ™‚

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Keywords filter’ is closed to new replies.