How to get post id of static front page?

I’m trying to use the front page as a default of sorts for featured images (if no featured image is set, I want to use the front page’s, for example)

But I’m having trouble finding out how to get the post ID of the front page in a safe manner (so that my code still works when the front page is inevitably changed by someone)

I know I could just hard code an ID in my code, but that’ll break when someone decides to use a new content item as the front page.

Would I have to use wp-query to achieve this? And if so, what is a safe way to achieve this with wp-query?

2

WordPress has a few useful options. You can get the homepage ID by using the following:

$frontpage_id = get_option( 'page_on_front' );

or the blog ID by using:

$blog_id = get_option( 'page_for_posts' );

Here’s a list of many useful get_option parameters.

Leave a Comment