How to get a page url by a page id?

I tried to use get_page_link('page-id') and get_permalink('page-id') but the error below occurred.

Fatal error: Call to a member function get_page_permastruct() on null in ...

How can I get a page url knowing only its ID?

Topic wp-link-pages permalinks links pages Wordpress

Category Web


<?php
$booking_url = get_permalink('15551');
echo $booking_url;
?>

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' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.