Make WordPress Core

Opened 4 years ago

Last modified 3 months ago

#14017 new enhancement

New template "tag": get_custom_field()

Reported by: filosofo Owned by: filosofo
Milestone: Future Release Priority: normal
Severity: normal Version: 3.0
Component: Posts, Post Types Keywords: has-patch 2nd-opinion
Focuses: template Cc:


It would be helpful to have a way to retrieve a custom field value that is somewhat agnostic of current context.

Current way to do this

In the header (i.e., before the Loop), one has to access the currently-queried object to get a custom value, with something like this:

$value = get_post_meta($GLOBALS['wp_query']->get_queried_object_id(), 'field', true);

In the Loop:
$value = get_post_meta(get_the_ID(), 'field', true);

And, lots of tutorials out there tell people to do things like the following, with varying degrees of success (depending on variable scope):

$value = get_post_meta($id, 'field', true);


$value = get_post_meta($post->ID, 'field', true);

My proposed function (or "template tag")

mixed get_custom_field ( string $fieldname [, int $post_id ] )

$value = get_custom_field('field');

It picks the current object like so:

Passed post object ID?
    /       \
   yes      no 
    |        |
   use it    |
          within Loop?
            /       \
          yes        no
           |          |
        use current   |
         Loop ID      |
                    currently queried
                    object is singular?
                    /           \
                   yes           no
                    |             | 
                use its ID      ID = 0

Attachments (1)

get_custom_value.14017.diff (1.0 KB) - added by filosofo 4 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 nacin3 years ago

  • Keywords commit added
  • Milestone changed from Awaiting Triage to 3.1

This makes a lot of sense. A friend was asking how to do this earlier, and I went through the same thought process as above when explaining it. +1

comment:2 filosofo3 years ago

Patch is still a thing of beauty.

comment:3 westi3 years ago

  • Keywords commit removed

Not convinced about the naming of this.

It really irks me that we call post meta by two different names and promoting that more here is not good IMHO.

Can we not just extend get_post_meta() to have this behaviour if passed a 0 id?

comment:4 filosofo3 years ago

I don't really care about the name, so please suggest one. However, they're called "Custom Fields" on the post edit page, so it seems like the reasonable name to me. The name of the db table the actual data is stored in is irrelevant to the person using the API, right?

I think changing get_post_meta to accept 0 as an argument like that is going to have serious backwards-compatibility issues, because get_post_meta should fail when given an invalid ID. And then they still have to add "true" to make sure they get a string.

My general idea is to make the lives of template designers easier.

comment:5 follow-up: nacin3 years ago

I don't care what we call it, but it's a very nifty function.


comment:6 in reply to: ↑ 5 markmcwilliams3 years ago

Replying to nacin:

I don't care what we call it, but it's a very nifty function.

Got to agree with you there! :)

comment:7 westi3 years ago

  • Keywords 3.2-early added
  • Milestone changed from 3.1 to Future Release

This is possibly a nifty idea.

But it is past the time for introducing new apis in 3.1

We should consider this for 3.2 though

comment:8 follow-up: scribu3 years ago

How about we make a WP_Post class, with a custom_field() method:

echo $post->custom_field( 'key' );

Of course, it could be extended with other methods too.

comment:9 in reply to: ↑ 8 mikeschinkel3 years ago

  • Cc mikeschinkel@… added

Replying to scribu:

How about we make a WP_Post class, with a custom_field() method:

echo $post->custom_field( 'key' );

Of course, it could be extended with other methods too.


comment:10 jaredatch2 years ago

  • Cc jared@… added
  • Keywords changed from has-patch, 3.2-early to has-patch 3.2-early

edit: not sure why its playing with the keywords

Last edited 2 years ago by jaredatch (previous) (diff)

comment:11 scribu2 years ago

  • Keywords 2nd-opinion added; 3.2-early removed

comment:12 nacin3 months ago

  • Component changed from Template to Posts, Post Types
  • Focuses template added
Note: See TracTickets for help on using tickets.