Random home page at each refresh between array of page IDs

I am trying to rotate 3 pages as my home page at refresh.

Currently I am using this in my functions.php:

?php
function my_homepage_redirect() {
    if ( is_home() || is_front_page() ){
        $page = get_posts( [
            'post_type' = 'page',
            'posts_per_page' = 1,
            'orderby' = 'rand',
            'fields' = 'ids'
        ] );

        if ( empty( $page ) ){
            return;
        }

        wp_redirect( get_permalink( $page[0] ) );
        exit;
    }
}
add_action( 'template_redirect', 'my_homepage_redirect' );
?

But I don't want a redirect. I'd like for the URL to stay domain.com.

Probably I need something like this (so that it just sets a page between the 3 as home page rather than redirecting), but how do I build this in functions.php?

Topic home-url page-template homepage templates Wordpress

Category Web


Not tested well but you can try this.

add_action( 'template_redirect', 'my_homepage_redirect' );
function my_homepage_redirect() {
    if ( is_home() || is_front_page() ){
        $page = get_posts( [
            'post_type' => 'page',
            'posts_per_page' => 1,
            'orderby' => 'rand',
            'fields' => 'ids'
        ] );
        
        if ( empty( $page ) ){
            return;
        }

        // update the front page id option according to the page id
        update_option( 'page_on_front',  $page[0]);
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.