[wp-trac] [WordPress Trac] #32275: get_page_by_title could return an array with all the matches

WordPress Trac noreply at wordpress.org
Thu May 7 03:49:41 UTC 2015


#32275: get_page_by_title could return an array with all the matches
-------------------------+------------------------------
 Reporter:  brgweb       |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Query        |     Version:  4.2.1
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:  template
-------------------------+------------------------------

Comment (by brgweb):

 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;
         }

 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/32275#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list