WordPress: How to get the ID for the Front Page and Home

Normally when you want to access the ID of a post to get the title you can simply reference the global $post; variable. However when you need to access the page ID for the Home template and Front Page template you need to reference two options from the database that store the ID for you.

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

By access these stored IDs you will then be able to use functions like get_the_title();

get_the_title($blog_id)

This is useful for adding content above your blog posts or fetching post meta for the home page and front page like so:

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

Which will then pull the meta information for the relevant key.

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

In Category: WordPress