customization – How can I make a search term redirect to a page

Question

You could use the template_redirect hook.

https://developer.wordpress.org/reference/hooks/template_redirect/

add_action('template_redirect', 'wpse410342_template_redirect');
function wpse410342_template_redirect() {
    
    if( !is_search() || is_admin() ) 
        return;
    
    global $wp_query;
    
    if( '123456' === $wp_query->get('s') )
        wp_redirect( get_permalink( get_page_by_title('Page Name') ) );
}

Using the page slug instead of name.

wp_redirect( get_permalink( get_page_by_path('page-name') ) );

0
Josh Woodhall 2 months 2022-10-11T17:15:26-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse