1

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' );
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • @brombeer within a WordPress system I don't think they deliberately included that file themselves, that is probably WP itself doing that. – CBroe Mar 30 '23 at 10:51
  • 2
    _"In post.php file:"_ - was that actually contained in there already - or did _you_ add it? I'm assuming it is probably the latter. You should not modify any WP core files to begin with. And neither should you try to "revive" deprecated functions, by simply "cloning" them. If you check the [documentation for the function](https://developer.wordpress.org/reference/functions/get_page_by_title/), it already says right on top there, that you should be using WP_Query instead. – CBroe Mar 30 '23 at 10:56
  • @CBroe I didn't make any changes to the core files myself. It just happened this morning :( – Jasmyin Yeap Mar 30 '23 at 11:01
  • 1
    Someone must have changed something ... Attempted a WP update that only halfway succeeded, or something like that. The `post.php` that comes with a current WP does _not_ contain that function, https://github.com/WordPress/WordPress/blob/master/wp-includes/post.php – CBroe Mar 30 '23 at 11:03
  • @CBroe I managed to reinstall WP and it works now. Thank you! – Jasmyin Yeap Mar 30 '23 at 14:11

1 Answers1

0

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.

Brian Johnson
  • 561
  • 1
  • 6
  • 17
  • I did the same way you said, but excluding only wp-content leads to missing all pages and layouts – MANSOOR KOCHY Apr 04 '23 at 15:50
  • You shouldn't delete your original content, or any files at all. You should still have your original wp-content folder. The reason I exclude wp-content from the new WP version install is because I don't want it to install default themes or plugins. – Brian Johnson Apr 05 '23 at 18:45