Make WordPress Core


Ignore:
Timestamp:
03/05/2014 10:25:31 PM (11 years ago)
Author:
johnbillion
Message:

Allow get_page_by_path() and get_page_by_title() to accept an array of post types. Fixes #24763. Props zbtirrell.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r27300 r27423  
    34983498 * @param string $page_path Page path
    34993499 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
    3500  * @param string $post_type Optional. Post type. Default page.
     3500 * @param string|array $post_type Optional. Post type or array of post types. Default page.
    35013501 * @return WP_Post|null WP_Post on success or null on failure
    35023502 */
    3503 function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
     3503function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
    35043504    global $wpdb;
    35053505
     
    35113511    $parts = array_map( 'sanitize_title_for_query', $parts );
    35123512
    3513     $in_string = "'". implode( "','", $parts ) . "'";
    3514     $post_type_sql = esc_sql( $post_type );
    3515     $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 );
     3513    $in_string = "'" . implode( "','", $parts ) . "'";
     3514
     3515    if ( is_array( $post_type ) ) {
     3516        $post_types = $post_type;
     3517    } else {
     3518        $post_types = array( $post_type, 'attachment' );
     3519    }
     3520
     3521    $post_types = esc_sql( $post_types );
     3522    $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
     3523    $sql = "
     3524        SELECT ID, post_name, post_parent, post_type
     3525        FROM $wpdb->posts
     3526        WHERE post_name IN ($in_string)
     3527        AND post_type IN ($post_type_in_string)
     3528    ";
     3529
     3530    $pages = $wpdb->get_results( $sql, OBJECT_K );
    35163531
    35173532    $revparts = array_reverse( $parts );
     
    35523567 * @param string $page_title Page title
    35533568 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
    3554  * @param string $post_type Optional. Post type. Default page.
     3569 * @param string|array $post_type Optional. Post type or array of post types. Default page.
    35553570 * @return WP_Post|null WP_Post on success or null on failure
    35563571 */
    3557 function get_page_by_title($page_title, $output = OBJECT, $post_type = 'page' ) {
     3572function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
    35583573    global $wpdb;
    3559     $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type ) );
     3574
     3575    if ( is_array( $post_type ) ) {
     3576        $post_type = esc_sql( $post_type );
     3577        $post_type_in_string = "'" . implode( "','", $post_type ) . "'";
     3578        $sql = $wpdb->prepare( "
     3579            SELECT ID
     3580            FROM $wpdb->posts
     3581            WHERE post_title = %s
     3582            AND post_type IN ($post_type_in_string)
     3583        ", $page_title );
     3584    } else {
     3585        $sql = $wpdb->prepare( "
     3586            SELECT ID
     3587            FROM $wpdb->posts
     3588            WHERE post_title = %s
     3589            AND post_type = %s
     3590        ", $page_title, $post_type );
     3591    }
     3592
     3593    $page = $wpdb->get_var( $sql );
     3594
    35603595    if ( $page )
    35613596        return get_post( $page, $output );
Note: See TracChangeset for help on using the changeset viewer.