Opened 3 years ago
Last modified 6 months ago
#12726 accepted enhancement
Add get_post_by_*() functions
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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)
Change History (10)
comment:1
mikeschinkel — 3 years ago
- Keywords post function removed
- Keywords needs-patch added
- Milestone changed from Unassigned to Future Release
- 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
Attached patch fixes a typo, and saves an sql query when $field is name.
comment:8
WraithKenny — 6 months ago
- Cc Ken@… added
- Keywords changed from has-patch, needs-testing to has-patch needs-testing

get_post_by() seems like a good idea.
Related, #12727 and #12743.