Opened 11 years ago
Closed 11 years ago
#32275 closed enhancement (invalid)
get_page_by_title could return an array with all the matches
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 4.2.1 |
| Component: | Query | Keywords: | close |
| Focuses: | template | Cc: |
Description
With the post_type support for get_page_by_title() it could be a good enhancement to return all the posts with that title, not just the oldest one (with the lower id).
Change History (4)
#2
@
11 years ago
- Keywords close added
The ability to pass an array of post types to this function was added in #24763 [27423].
We can't change simply change the function to return an array of found posts rather than a single post. This change would break backward compatibility for all plugins using the function. We could possibly introduce a $single parameter, like get_metadata() has; the default, true, would return the first matched post, while false would return an array of matched posts.
get_page_by_title() seems to me like a poor workaround for the fact that WP_Query doesn't allow queries by 'post_title'. I'm assuming this was never implemented because of performance concerns - the post_title column in the posts table is not indexed. That being said, it seems like a better use of effort to work toward 'post_title' support in WP_Query than to continue to make get_page_by_title() into an all-purpose function. (It's worth noting that get_page_by_title() is not used in WP core.)
A side note: much the same could be said about get_page_by_path(), though that function does have a special purpose: it can parse a complex/path/with/slashes and turn it into a 'post_name'. Since [30158] #18962, it's possible for posts in different post types to share the same 'post_name'.
I'm suggesting that we close as wontfix, but happy to hear other points of view.
#3
follow-up:
↓ 4
@
11 years ago
Thank you for your comment. It's my first time here in core development.
The problem I've found here was that I needed to query attachments with the same original filename. Since the filename is changed in the upload and the post-name is changed too, both to prevent duplicates, the easiest way to find it was querying by post_title (assuming that the user didn't change it after upload).
The other option was to query the post-names removing the '-x' in theend, where x is the character added to prevent duplicates, but it would result in a bigger code.
Maybe a better support to find attachments by its original filename would solve this kind of issue (don't know if it's a common issue...).
#4
in reply to:
↑ 3
@
11 years ago
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Status changed from new to closed
Thank you for your comment. It's my first time here in core development.
Thanks for the ticket, and welcome :)
The problem I've found here was that I needed to query attachments with the same original filename. Since the filename is changed in the upload and the post-name is changed too, both to prevent duplicates, the easiest way to find it was querying by post_title (assuming that the user didn't change it after upload).
Gotcha. I guess the issue of '-x' is why you can't query against '_wp_attached_file' postmeta? WP doesn't store an unmodified version of the filename anywhere that I can see. If you need it, I'd suggest hooking into the file upload process somewhere - maybe at 'wp_handle_upload'? - and storing it yourself, in a way that'll be easy to query.
Using 'post_title' to look this up is, IMO, a fragile hack, but that doesn't mean that fetching multiple items by 'post_title' doesn't have relevant use cases. That being said, I do think it's better suited as an improvement for WP_Query than here. If you think this is worth pursuing, please feel free to open a separate enhancement ticket.
I did a quick patch that worked for me.
function new_get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { global $wpdb; $return = array(); if ( is_array( $post_type ) ) { $post_type = esc_sql( $post_type ); $post_type_in_string = "'" . implode( "','", $post_type ) . "'"; $sql = $wpdb->prepare( " SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type IN ($post_type_in_string) ", $page_title ); } else { $sql = $wpdb->prepare( " SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = %s ", $page_title, $post_type ); } $page_col = $wpdb->get_col( $sql ); if ( $page_col ){ foreach ($page_col as $page){ $return[] = get_post( $page, $output ); } }else{ $return = null; } return $return; }