WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 11 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:

Description

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

or

$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 5 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 @nacin4 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 @filosofo4 years ago

Patch is still a thing of beauty.

comment:3 @westi4 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 @filosofo4 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: @nacin4 years ago

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

get_post_meta_field()?

comment:6 in reply to: ↑ 5 @markmcwilliams4 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 @westi4 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: @scribu4 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 @mikeschinkel4 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.

+1

comment:10 @jaredatch3 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 3 years ago by jaredatch (previous) (diff)

comment:11 @scribu3 years ago

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

comment:12 @nacin15 months ago

  • Component changed from Template to Posts, Post Types
  • Focuses template added

comment:13 @obenland11 months ago

There is a way to retrieve a custom field value that is somewhat agnostic of current context: post_custom() (#).

I suppose we could introduce a function that is a bit more coherent with the naming conventions of template tags, and which optionally also accepts a post id:

<?php
/**
 * Retrieve the value for a post meta field.
 *
 * @since 4.0.0
 *
 * @param string $key The meta key to retrieve.
 * @param int $post_id Optional. Post ID. 
 *
 * @return mixed The value of meta data field or an empty string.
 */
function get_post_meta_value( $key, $post_id = 0 ) {
        return get_post_meta( get_post( $post_id )->ID, $key, true );
}

But one could make the argument that it is not strictly necessary. If they're looking for a meta value of a specific post, they can use get_post_meta(). If they're looking for a meta value of the current post, they can use post_custom().

Last edited 11 months ago by obenland (previous) (diff)
Note: See TracTickets for help on using tickets.