To set a Home Page and Post Page, changes are normally submitted manually in the Reading Settings page of the WP Admin Dashboard. With the code snippet below, these changes can be made programmatically.
The two variables, $home_page
and $blog_page
, should match the title of a Page you create in the WP Dashboard.
<?php | |
/** | |
* Update WordPress Reading Settings Programmatically. | |
* | |
* @author Edwin Velez | |
* @see https://edwinvelez.net/programmatically-set-reading-settings-in-wp-admin/ | |
*/ | |
// Set static front page. | |
$home_page = get_page_by_title('Home'); | |
update_option('page_on_front', $home_page->ID); | |
update_option('show_on_front', 'page'); | |
// Set blog page. | |
$blog_page = get_page_by_title('Blog'); | |
update_option('page_for_posts', $blog_page->ID); |