[wp-trac] [WordPress Trac] #14017: New template "tag": get_custom_field()
WordPress Trac
wp-trac at lists.automattic.com
Mon Jun 21 04:13:04 UTC 2010
#14017: New template "tag": get_custom_field()
-------------------------+--------------------------------------------------
Reporter: filosofo | Owner: filosofo
Type: enhancement | Status: new
Priority: normal | Milestone: 3.1
Component: Template | Version: 3.0
Severity: normal | Keywords: has-patch
-------------------------+--------------------------------------------------
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
}}}
--
Ticket URL: <http://core.trac.wordpress.org/ticket/14017>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list