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?

  • Are you writing it like that with 'page-id'? You need to pass an actual id to it and it should work. get_permalink(4); or a variable with the id get_permalink($id); – jzatt Mar 12 '15 at 16:29
  • yes, I know. I wrote 'page-id' as an example. The problem is that it doesn't work with an existing page id. I tried it also in a fresh installation of WP. – Maverick Mar 12 '15 at 16:32
  • Good, you can never be sure so I thought I'd atleast ask! :) – jzatt Mar 13 '15 at 9:49

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.