Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#28243 closed defect (bug) (worksforme)

If a static page is used as the "Posts page", the page's metadata is not availible

Reported by: kelderic's profile Kelderic Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Query Keywords:
Focuses: template Cc:

Description

To Reproduce:

  1. Set up a WordPress install.
  2. Create a static page.
  3. In Settings -> Reading, Set "Posts page:" to your created static page.
  4. Give your static page any custom metadata.
  5. Attempt to access this metadata via index.php using get_post_meta( $post->ID, ... )

Expected Results:

Selected metadata should appear.

Actual Results:

No metadata appears.

Possible Cause

I think this is because the $post is being replaced by all the posts which are being displayed. We should be able to grab the actual page's metadata though.

Change History (3)

#1 @helen
12 years ago

  • Keywords close added

Give get_queried_object_id() a shot. In general you'll have a better time with that than global $post.

#2 @Kelderic
12 years ago

That does work on index.php, thanks! Also tested get_the_ID(), and it behaves the same as $post->ID:

$post->ID - Replaced by sub loop on index.php.
get_the_ID() - Replaced by sub loop on index.php
get_queried_object_id() - Correctly grabs page's ID on index.php

The Codex documentation of get_post_meta() mentions using $post->ID and get_the_ID(), but not get_queried_object_id(). Adding something there would clarify which to use under different circumstances. Would I need to open a new ticket?

#3 @helen
12 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

get_the_ID() is indeed a function to be used within The Loop, and since the loop on that page is that of the latest posts, it is returning what it's supposed to.

Codex can be edited by anybody who is logged in. I think it would be sensible to make a note about getting the ID that you want, but perhaps not too specific, and remembering that get_post_meta() is not where you learn about how to get the ID of the object you want.

Note: See TracTickets for help on using tickets.