WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 years ago

#14019 closed enhancement (wontfix)

Introduce get_id_by_name()

Reported by: filosofo Owned by: filosofo
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Template Keywords: has-patch dev-feedback
Focuses: Cc:

Description

Since 2.9-ish there has been an emphasis on querying objects by slug, for various reasons (especially terms).

Unfortunately, many template functions require either that one be in the Loop or explicitly pass a post object ID. Currently, to get an object's ID in order to pass it to one of the functions, you have to go through several steps that include querying the object and checking for the necessary properties of the desired result.

Proposed New Function

int get_id_by_name ( string $name_or_slug )

Examples:

// title of the page with slug "gallery":
echo get_the_title( get_id_by_name( 'gallery' ) );

// custom field for post titled "My Day at the Beach"
echo get_post_meta( get_id_by_name( 'my-day-at-the-beach' ), 'photos-url', true );

// images attached to the "Employees" page:
$images = get_children( array('post_parent' => get_id_by_name( 'employees' ), 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image' ) );

Attachments (1)

get_id_by_name.14019.diff (926 bytes) - added by filosofo 4 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 filosofo4 years ago

  • Owner set to filosofo
  • Status changed from new to accepted

comment:2 toscho4 years ago

  • Cc toscho added

If it helps … I wrote a similar function: http://toscho.de/2010/wordpress-id-anhand-titel-herausfinden/

It’s caching the result, and it can find the ID for any taxonomy too.

comment:3 filosofo4 years ago

  • Milestone changed from Awaiting Triage to 3.1

comment:4 follow-up: Utkarsh4 years ago

  • Cc admin@… added

I would suggest implementing a get_post_by (#12726) instead of this.

comment:5 in reply to: ↑ 4 filosofo4 years ago

Replying to Utkarsh:

I would suggest implementing a get_post_by (#12726) instead of this.

That would not address the problem, which is that you have to go through a couple of steps to get the desired post's ID. As the original example demonstrates, what we can't currently do is something like this:

// custom field for post titled "My Day at the Beach"
echo get_post_meta( get_id_by_name( 'my-day-at-the-beach' ), 'photos-url', true );

comment:6 ericmann4 years ago

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.

comment:7 filosofo4 years ago

Method chaining is great, but it's been available only since PHP 5, and WP has to be compatible with 4.3.

comment:8 filosofo3 years ago

  • Keywords dev-feedback added

Can I get some commit dev feedback on this? I know this would be invaluable for me, with the custom themes I make, so I imagine it would be useful for others.

Patch is still fresh.

comment:9 nacin3 years ago

I would like to implement get_post_by() at some point, but in the meantime this sounds like it is handled by get_page_by_path(), so I'm not sure we need this. Am I mistaken?

comment:10 filosofo3 years ago

I'm really trying to pass it to stuff that takes IDs, and not everything that takes IDs takes objects.

comment:11 nacin3 years ago

Okay, understood. Missed the point about chaining.

(Aside: get_id_by_name() is very, um, nonspecific. I thought you were referring to terms at first given that you started the ticket by talking about terms.)

It seems like it solves a relatively minor use case, as it's just slightly more code to get the ID from the object, and come 3.2 we'll be able to chain all we want, so the function basically becomes moot 6 months after it is introduced.

comment:12 filosofo3 years ago

  • Milestone 3.1 deleted
  • Resolution set to wontfix
  • Status changed from accepted to closed

OK, fair enough.

Note: See TracTickets for help on using tickets.