Quickly access a page's object in WordPress

Published March 10, 2019
.* :☆゚

Sometimes accessing the page ID is a lot more convoluted than you thought, especially if you’re outside loops.

Using ` get_page_by_path();` or ` get_page_by_title();` is an easy way to access a specific page object and is helpful when you know the slug or title of a page will likely never change.

<?php

   $page = get_page_by_path( 'contact' );
   // OR
   $page = get_page_by_title( 'Contact Us' );

   echo $page->title;
   // will display the Contact page title

?>

You can also use get_page_by_title() to specify a page name instead:

<?php

   $page = get_page_by_title( 'About Us' );

?>