[wp-trac] [WordPress Trac] #14019: Introduce get_id_by_name()
WordPress Trac
wp-trac at lists.automattic.com
Mon Oct 18 18:53:35 UTC 2010
#14019: Introduce get_id_by_name()
-------------------------+--------------------------------------------------
Reporter: filosofo | Owner: filosofo
Type: enhancement | Status: accepted
Priority: normal | Milestone: 3.1
Component: Template | Version: 3.0
Severity: normal | Keywords: has-patch
-------------------------+--------------------------------------------------
Comment(by ericmann):
I'd much rather focus on one of two options:
'''1. Make the original functions more flexible.'''
Right now, functions like `get_the_title()` take in a post ID and return a
title. If it could take in ''either'' a post ID or a post slug, this
would be more flexible and powerful.
'''2. Use a `get_post_by` instead'''
The return of a `get_post_by` can be an object or associative array with
most of the data you need. Responding to your specific examples:
{{{
// title of the page with slug "gallery":
echo get_post_by_slug( 'gallery' )->the_title;
// custom field for post titled "My Day at the Beach"
echo get_post_meta( get_post_by_slug( 'my-day-at-the-beach' )->ID,
'photos-url', true );
// images attached to the "Employees" page:
$images = get_children( array('post_parent' => get_post_by_slug(
'employees' )->ID, 'post_status' => 'inherit', 'post_type' =>
'attachment', 'post_mime_type' => 'image' ) );
}}}
IMO Building quick one-off functions to return simple data will just bog
things down and cause problems. If you take an object-oriented approach
you can store most of this data in an object and just reference it when
needed.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/14019#comment:6>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list