Make WordPress Core

Ticket #24763: get_page_by_path_v4.patch

File get_page_by_path_v4.patch, 2.7 KB (added by zbtirrell, 11 years ago)

this patch also adds post type array support to get_page_by_title

  • wp-includes/post.php

     
    35133513 *
    35143514 * @param string $page_path Page path
    35153515 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
    3516  * @param string $post_type Optional. Post type. Default page.
     3516 * @param string|Array $post_type Optional. Post type or array of post types. Default page.
    35173517 * @return WP_Post|null WP_Post on success or null on failure
    35183518 */
    35193519function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
     
    35273527        $parts = array_map( 'sanitize_title_for_query', $parts );
    35283528
    35293529        $in_string = "'". implode( "','", $parts ) . "'";
    3530         $post_type_sql = esc_sql( $post_type );
    3531         $pages = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_type FROM $wpdb->posts WHERE post_name IN ($in_string) AND (post_type = '$post_type_sql' OR post_type = 'attachment')", OBJECT_K );
    35323530
     3531        if ( is_array( $post_type ) ) {
     3532                $post_types = $post_type;
     3533        }
     3534        else {
     3535                $post_types = array( $post_type, 'attachment' );
     3536        }
     3537        $post_types = esc_sql( $post_types );
     3538
     3539        $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
     3540
     3541        $pages = $wpdb->get_results( $sql = "SELECT ID, post_name, post_parent, post_type FROM $wpdb->posts WHERE post_name IN ($in_string) AND post_type IN ($post_type_in_string)", OBJECT_K );
     3542
    35333543        $revparts = array_reverse( $parts );
    35343544
    35353545        $foundid = 0;
     
    35673577 *
    35683578 * @param string $page_title Page title
    35693579 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
    3570  * @param string $post_type Optional. Post type. Default page.
     3580 * @param string|Array $post_type Optional. Post type or array of post types. Default page.
    35713581 * @return WP_Post|null WP_Post on success or null on failure
    35723582 */
    35733583function get_page_by_title($page_title, $output = OBJECT, $post_type = 'page' ) {
    35743584        global $wpdb;
    3575         $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type ) );
    3576         if ( $page )
    3577                 return get_post( $page, $output );
    35783585
    3579         return null;
     3586        if ( is_array( $post_type ) ) {
     3587                $post_type = esc_sql( $post_type );
     3588                $post_type_in_string = "'" . implode( "','", $post_type ) . "'";
     3589
     3590                $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type IN ($post_type_in_string)", $page_title );
     3591        }
     3592        else {
     3593                $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type );
     3594        }
     3595
     3596        $page = $wpdb->get_var( $sql );
     3597
     3598        if ( ! $page ) {
     3599                return null;
     3600        }
     3601
     3602        return get_post( $page, $output );
    35803603}
    35813604
    35823605/**