WordPress: Get the ID for the Front Page and Home

WordPress

Normally in WordPress if you wanted to access an ID for a post or page you would reference the global $post variable. However, what if you wanted to access the ID for the Home and Front Page templates?

In that case you will need to reference the stored ID from the WordPress database using the get_option function.

Using WordPress get_option

This function is used to quickly reference a specific option from the database.

$front_id = get_option('page_on_front');
$blog_id = get_option('page_for_posts');

By accessing these stored IDs you will then be able to use functions like get_the_title() to store the title for example.

$title = get_the_title($blog_id)

You can also use the front page and home ID to access meta information.

$front_meta = get_post_meta($front_id, '_meta_key', true);

Remember these two options, they will save you a tonne of time.

Matthew Horne

Matthew is a web developer from the United Kingdom who taught himself PHP and JavaScript and never looked back. If you would like to hire me, shoot me an email.