So how to redirect category to a page of the same name if it exists?

get_page_by_path does something similar:

function pagefromcat_template_redirect()
{
    if ( ! is_category() ) {
        return;
    }
    $category = get_queried_object();
    $page = get_page_by_path( $category->slug );
    if ( $page instanceof WP_Post )
    {
        $url = get_permalink( $page );
        wp_safe_redirect( $url, 301 );
    }

}

add_action( 'template_redirect', 'pagefromcat_template_redirect' );

Leave a Comment