How to get a page url by a page id?

You’re probably getting that error because WordPress doesn’t have the $wp_rewrite global loaded yet for some reason. Either something deactivated it, or you’re trying to run those functions before WordPress has a chance to load it.

If you’re trying to do this in a plugin or in your theme’s functions.php file, make sure you’re inside a function that is hooked to after_setup_theme or a hook that runs sometime after. For example:

function get_url_of_page_id_165() {
    return get_permalink( 165 );
}
add_action( 'after_setup_theme', 'get_url_of_page_id_165' );

Leave a Comment