When I try to use get_page() function I get the error message:

Only variables can be passed by reference


But what doesn't make sense is that I can use a work around that blows my mind:

First test

$test1 = get_page(49);

Returns error message

Only variables can be passed by reference


Second test

$page = get_page_by_title( 'About' );
$test2 = get_page($page->ID);
echo 'ID:'.$page->ID.' Post title: '.$page->post_title;

Returns no error

ID:49 Post title: About


Can anyone shed some light on what I'm doing wrong?


Correct to your first test:

$id = 49;
$test = get_page( $id );
  • Cheers, feeling a bit silly right now :) – jamiers May 5 '12 at 17:09
  • @jamiers Do not think about the bad, you are probably tired .. – user15194 May 5 '12 at 17:17

You're seeing the expected behavior of the get_page() function. From the function's codex page:

$page_id = 123; // 123 should be replaced with a specific Page's id from your site, which you can find by mousing over the link to edit that Page on the Manage Pages admin page. The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123.

$page_data = get_page( $page_id ); // You must pass in a variable to the get_page function. If you pass in a value (e.g. get_page ( 123 ); ), WordPress will generate an error. By default, this will return an object. [Emphasis added]

So just save the page_id as a variable and pass it in (as the other answer suggests).

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.