Opened 3 years ago

Last modified 6 months ago

#12726 accepted enhancement

Add get_post_by_*() functions

Reported by: mikeschinkel Owned by: sorich87
Priority: normal Milestone: Future Release
Component: Post Types Version: 3.0
Severity: normal Keywords: has-patch needs-testing
Cc: kevinB, sorich87@…, admin@…, Ken@…

Description

Current there are get_page_by_path() and get_page_by_title() function but they hardcode the post_type of 'page'. With support for new custom post types I'm finding a need for functionality to look up posts of custom post types:

$args = array('post_type','my_custom_post_type');
$path = 'foo-bar';
$post = get_post_by_path($path,$args);
$title = 'Foo Bar'
$post = get_post_by_title($title,$args);

Another option would be a simple get_post_by():

$args = array('post_type','my_custom_post_type');
$path = 'foo-bar';
$post = get_post_by('path',$path,$args);
$title = 'Foo Bar'
$post = get_post_by('title',$title,$args);

This code is not hard to write but looking at the functions in post.php there's not one consistent style so I'm not sure what the best approach would be to write it. Further, I don't completely understand the significance of all the code in get_page_by_path() so wouldn't want to start with it (although I could see it being modified to use the more generic functions that I propose.)

I can make these updates if I get enough direction from the core team, or I'd happily just see them get done. :)

Attachments (2)

post.patch (4.6 KB) - added by sorich87 3 years ago.
12726.patch (4.6 KB) - added by Utkarsh 3 years ago.

Download all attachments as: .zip

Change History (10)

  • Keywords post function removed
  • Keywords needs-patch added
  • Milestone changed from Unassigned to Future Release

get_post_by() seems like a good idea.

Related, #12727 and #12743.

  • Cc kevinB added
  • Cc sorich87@… added
  • Keywords has-patch added; needs-patch removed

I added a patch which adds get_post_by().
Should we deprecate get_page_by_path(), get_page_by_title()?

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

Related #10865

  • Cc admin@… added
  • Keywords needs-testing added

Utkarsh3 years ago

Attached patch fixes a typo, and saves an sql query when $field is name.

  • Cc Ken@… added
  • Keywords changed from has-patch, needs-testing to has-patch needs-testing
Note: See TracTickets for help on using tickets.