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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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' );
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x