In functions.php is it possible to dynamically set the static home page?

The condition could be anything, but for example by domain:

if ( $_SERVER['HTTP_HOST'] == 'domain-one.com' )
else if ( $_SERVER['HTTP_HOST'] == 'domain-two.com' )

Note that it should not permanently change the home page set in the WordPress Settings, only for the current page load. I want different visitors to see different homepages.

Any approach welcome that can neatly override the default home page.

  • Are the two pages (using page templates I assume ) completely different? What was the reasoning behind using page templates instead of using the built in home.php and front-page.php templates?
    – Tom J Nowell
    Jul 18 '15 at 1:09
  • And ... you have two domains loading the same WordPress instance?
    – s_ha_dum
    Jul 18 '15 at 4:36
  • Yep multiple (actually more than 2) domains. Code could potentially go in front-page.php instead, was hoping it was possible to choose the static home page by ID or slug.
    – Ben
    Jul 18 '15 at 8:33

Try the template_include filter. See the docs for code example.


This is the best solution I've been able to come up with, and satisfies the need to dynamically set the home page (not template) based on factors such as subdomain.

add_action( 'pre_get_posts', 'dynamic_home' );
function dynamic_home( $query ) {
  if ( is_front_page() && $query->is_main_query() ) {
    $query->set( 'post_type' , 'my-cpt' );
    $query->set( 'name' , 'a-slug' );
    $query->set( 'p' , null );
    $query->set( 'page_id' , null );

In the above some_custom_condition() could contain logic such as if the current URL is the home page.

This example overrides the home page with a custom post type, but could be simpler if just page content types were used.

Setting the template alone is not sufficient and does not scale to potentially unlimited number of home pages. Such a scenario would be each user of a site having their own personalised home page on their own subdomain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.