Programmatically set page_on_front

I am trying to programmatically set the ‘page_on_front’ option with the id value retrieved from the get_page_by_title command…

$homepage = get_page_by_title( 'Front Page' );
update_option('page_on_front', $homepage);
update_option('show_on_front', 'page');

This isn’t working, can anyone help?

1

get_page_by_title() returns an object.

Use $homepage->ID.

You should also check if you really got a usable return value:

$homepage = get_page_by_title( 'Front Page' );

if ( $homepage )
{
    update_option( 'page_on_front', $homepage->ID );
    update_option( 'show_on_front', 'page' );
}

Leave a Comment