admin管理员组文章数量:1277290
In my plugin, I need to find any posts of a custom post type that have a certain title. I was using this:
$my_post = get_page_by_title( $title, OBJECT, 'my_custom_post_type' );
This works great, but if there is more than one post with the same title, get_page_by_title will only return one result. According to the Codex article on get_page_by_title, this is the correct behavior for the function.
How do I retrieve all the posts with a given title, rather than just one?
In my plugin, I need to find any posts of a custom post type that have a certain title. I was using this:
$my_post = get_page_by_title( $title, OBJECT, 'my_custom_post_type' );
This works great, but if there is more than one post with the same title, get_page_by_title will only return one result. According to the Codex article on get_page_by_title, this is the correct behavior for the function.
How do I retrieve all the posts with a given title, rather than just one?
Share Improve this question asked Jul 24, 2013 at 21:08 Ben MillerBen Miller 2,1403 gold badges19 silver badges41 bronze badges2 Answers
Reset to default 5You will need to create a new function. My example is a fork of the core function. The following will allow you to create a query across all published posts regardless of post_type, unless you desire that specific set.
function get_posts_by_title($page_title, $post_type = false, $output = OBJECT ) {
global $wpdb;
//Handle specific post type?
$post_type_where = $post_type ? 'AND post_type = %s' : '';
//Query all columns so as not to use get_post()
$results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_title = %s $post_type_where AND post_status = 'publish'", $page_title, $post_type ? $post_type : '' ) );
if ( $results ){
$output = array();
foreach ( $results as $post ){
$output[] = $post;
}
return $output;
}
return null;
}
//This should get you an array of all posts with 'Foo Bar' as the title
get_posts_by_title('Foo Bar');
I searched around the internet too for this and couldn't find anything, so to me, the most valuable part of this question is verifying that nothing better is built into Wordpress. The following is the best way I know.
The main difference between this and the marked answer is that it returns real WP_Post
objects, and doesn't ignore the $output
parameter.
function get_pages_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
global $wpdb;
$prepared = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = %s AND post_status = 'publish' ORDER BY ID", $page_title, $post_type );
$results = $wpdb->get_results( $prepared );
$post_ids = array_map( function($x) { return intval($x->ID); }, $results );
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => $post_type,
'post__in' => $post_ids,
'ignore_sticky_posts' => true,
'orderby' => 'ID'
) );
return $posts;
}
(Note: I didn't test this code directly. It came from code I wrote that's more specific to my project. If you find any obvious errors, please fix!)
本文标签: pluginsHow do I get multiple pages by title
版权声明:本文标题:plugins - How do I get multiple pages by title? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741291931a2370600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论