Cannot redeclare get_page_by_title()

1.5k Views Asked by At

I'm not sure what happened, there's a fatal error on my website:

Fatal error: Cannot redeclare get_page_by_title() (previously declared in /home/hamburgerasia/public_html/wp-includes/post.php:5778) in /home/hamburgerasia/public_html/wp-includes/deprecated.php on line 4560

In post.php file:

function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
    global $wpdb;

    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 = $wpdb->get_var( $sql );

    if ( $page ) {
        return get_post( $page, $output );
    }

    return null;
}

In deprecated.php file:

function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
    _deprecated_function( __FUNCTION__, '6.2.0', 'WP_Query' );
    global $wpdb;

    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 = $wpdb->get_var( $sql );

    if ( $page ) {
        return get_post( $page, $output );
    }

    return null;
}

Please help, thank you!

I tried the following and it did not work as well :( In deprecated.php file:

include ('post.php');
function get_page_by_title() {
    _deprecated_function( __FUNCTION__, '6.2.0', 'WP_Query' );
}
1

There are 1 best solutions below

2
Brian Johnson On

In short, I believe you had a failed WordPress core update. The solution is to re-upload the newest version of WordPress manually.

I had the same problem after updating the WordPress core. I believe that it timed out for whatever reason and did not complete, leading to that exact error.

The solution was to simply manually upload the new WP version again. I usually just create a zip file that contains all the files in the first level of the zip and excludes /wp-content/, and then upload and unzip it directly.

FTP would work, too, but would just generally take longer.