# How to add a custom filter in functions.php

I have been through the Wordpress codex but I can't figure out how to write a filter.

I would like to use a filter called: 'wpseo_sitemap_' . $filter . '_change_freq'. It expects you to return one of the following values: 'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never' I would like to set this to 'hourly' ## 2 Answers To be honest, I had to actually see the source code to get the correct answer for you. The problem with this particular filter is that the $filter part is dynamic and thus you have to know what particular frequency you want to modify. There's a numerous of options:

homepage,

blogpage,

$post_type . '_archive', $post_type . '_single',

$c->taxonomy . '_term', author_archive. So once you figure out what particular filter you want to modify (I have chosen a homepage one), you put the following to your functions.php add_filter( 'wpseo_sitemap_homepage_change_freq', 'my_custom_homepage_freq', 10, 2 ); function my_custom_homepage_freq($default, $url ) { return 'hourly'; }  Changing author_archive is also easy one, as you replace the $filter part in the filter name to author_archive.

In case of $post_type . '_single' (or the other one with $post_type in name), you have to replace the $filter part with the post_type name. Eg for pages only: add_filter( 'wpseo_sitemap_page_single_change_freq', 'my_custom_page_freq', 10, 2 ); function my_custom_page_freq($default, $url ) { return 'hourly'; }  To reveal you the part of figuring out what can stand for $filter in your original question, I had to check out the ./inc/class-sitemaps.php file (can be seen here in Subversion: http://plugins.svn.wordpress.org/wordpress-seo/trunk/inc/class-sitemaps.php ) where on the line 578 is the function called filter_frequency and it constructs the filter. That function filter_frequency is being called few times from within the file and contains the $filter part as it's first argument. Eg: $this->filter_frequency( 'homepage', 'daily', $this->home_url )  • thank you David for the detailed answer. Is it possible to change everything simply to 'daily' with the most simple code as possible? By everything I mean, homepage, posts and all custom post types – Joanna Mikalai Sep 28 '14 at 16:11 • the other solution would be to change the file you are mentioning to 'daily' at 3 different locations but I will have to do that everytime I update the plugin – Joanna Mikalai Sep 28 '14 at 16:26 • @david.binda Thanks for your answer. Just a simple question for you: add_filter( 'wpseo_sitemap_homepage_change_freq', 'my_custom_homepage_freq', 10, 2 ); what does the 10 and 2 parameters represent ? Are they the ids of your page ? Thanks ! – sambellerose Mar 29 '16 at 22:12 • @sambellerose the 10 and 2 represent a priority of the filter (10) and number of arguments passed to the callback (2). See developer.wordpress.org/reference/functions/add_filter for more details – david.binda Mar 30 '16 at 18:48 I know the answer is late but there is great article written by one blogger. As explained in the link 'wpseo_sitemap_' .$filter . '_change_freq'


the $filter should be replaced with post type that you want to change, you can replace it with: • homepage, • blogpage, • $post_type . '_archive',

• $post_type . '_single', • $c->taxonomy . '_term',

• author_archive.

while the $post_type variable based on WordPress Codex can be replaced with: • Post (Post Type: 'post') • Page (Post Type: 'page') • Attachment (Post Type: 'attachment') • Revision (Post Type: 'revision') • Navigation menu (Post Type: 'nav_menu_item') at the end for single post your code should look like: add_filter( 'wpseo_sitemap_page_single_change_freq', 'my_custom_page_freq', 10, 2 ); function my_custom_page_freq($default, \$url )
{
return 'hourly';
}


After adding this to your theme functions go back to Yoast Seo plugin back-end, disable XML sitemap functionality, save it, then re-enable and reload www.yoursite.com/page-sitemap.xml

• Welcome to WP.SE! Link only answers are discouraged, please make an edit to include the detail of the article you're linking to. Your answer may be flagged as spam and removed, too. – DᴀʀᴛʜVᴀᴅᴇʀ Feb 12 '16 at 18:42
• Answer has been updated .verify it as i implemented the same code and it is working – Anubhav Feb 13 '16 at 5:43
• How is your answer different than the existing one? – fuxia Feb 13 '16 at 5:57