the_title and document_title in a single call are causing issues on menu items

I'm trying to replace the_title and document_title on dynamic pages like ?filter=most-viewed, it works but only problem is the menu items are getting replaced with custom title too. add_filter( 'document_title', 'custom_title', 10 ); add_filter('the_title', 'custom_title', 10); I don't want to write 2 functions for each call so my function is: function custom_title( $title ) { $filter = $_REQUEST['filter']; if($filter=='most-viewed'){ $title = 'Most Viewed'; } //Fix issue in the loop if (in_the_loop() ){ global $wp_query; return $wp_query->post->post_title; } return $title; } I tried to add below code but does not work if (is_nav_menu() ){ global $wp_query; return $wp_query->post->post_title; } If I add all function inside if (in_the_loop() ){ it resolves the issue on menus but the document_title won't work.

Comment (1)

Jese Leos

23 hours ago

Verified user

Within the callback, you can add remove_action( current_action(), 'custom_title', 10 ), limiting each action to running only once.

You’ll be in good company