1

I have ran a php code to remove some pages of the database, the code worked fine and i have left with the optmised pages i needed.

However, now i can’t create new pages at all.

This happened with 2 sites now, The first website (catering.jobs), the fornt end gives:

        Warning: Attempt to read property "post_type" on null in /wp-includes/post.php on line 7618

        Warning: Attempt to read property "post_type" on null in /wp-includes/post.php on line 7619

        Warning: Attempt to read property "ID" on null in /wp-includes/post.php on line 7623

        Warning: Attempt to read property "post_type" on null in /wp-includes/post.php on line 7868

        Warning: Attempt to read property "post_type" on null in /wp-includes/theme.php on line 3532

        Warning: Attempt to read property "post_type" on null in /wp-includes/theme.php on line 3757

        Warning: Attempt to read property "post_type" on null in /wp-includes/post.php on line 5405

        Warning: Attempt to read property "ID" on null in /wp-includes/post.php on line 5405

        Warning: Attempt to read property "post_type" on null in /wp-includes/post.php on line 4706

        Warning: Attempt to read property "post_type" on null in /wp-admin/includes/post.php on line 709

        Fatal error: Uncaught Error: Attempt to assign property "post_content" on null in /wp-admin/includes/post.php:748 
        Stack trace: #0 /wp-admin/post-new.php(66): get_default_post_to_edit('page', true) #1 {main} thrown in /wp-admin/includes/post.php on line 748

The error log gives:

         Got error '
    PHP message: PHP Warning:  Attempt to read property "post_type" on null in /wp-includes/post.php on line 7618
    PHP message: PHP Warning:  Attempt to read property "post_type" on null in /wp-includes/post.php on line 7619
    PHP message: PHP Warning:  Attempt to read property "ID" on null in /wp-includes/post.php on line 7623
    PHP message: PHP Warning:  Attempt to read property "post_type" on null in /wp-includes/post.php on line 7868
    PHP message: PHP Warning:  Attempt to read property "post_type" on null in /wp-includes/theme.php on line 3532
    PHP message: PHP Warning:  Attempt to read property "post_type" on null in /wp-includes/theme.php on line 3757

On the second website (recruitment-agency.london), the frontend gives:

        Fatal error: Uncaught Error: Attempt to assign property "post_content" on null
    in /wp-admin/includes/post.php on line 748

    Call stack:

    get_default_post_to_edit()
    wp-admin/post-new.php:66

And the error log gives:

        Got error '
    PHP message: PHP Fatal error:  Uncaught Error: Attempt to assign property "post_content" on null in /wp-admin/includes/post.php:748\n
    Stack trace:\n#0 /wp-admin/post-new.php(66): get_default_post_to_edit('page', true)\n#1 {main}\n  thrown in /wp-admin/includes/post.php on line 748'

I have done the following:

1- Cleared the cache on server and browser

2- Tried with 2020 theme with no plugins active.

3- Repaired and optimised the database tables.

4- Reinstalled wordpress the latest version.

5- Contatced my hosting support and they had no idea.

Can anyone give me a hint please?

Update: the php code to remove the not optimised pages is here:

        <?php
    // Read ral-opt-slugs.txt file and store slugs in array
    $slugs = file('ral-opt-slugs.txt', FILE_IGNORE_NEW_LINES);

    // Connect to database
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "test";

    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    // Ping the MySQL server to ensure the connection is still alive
    if (!mysqli_ping($conn)) {
        die("Connection lost.");
    }

    // Delete pages that are not in the list of slugs and not reserved
    $sql = "SELECT * FROM wp_posts WHERE post_type = 'page' AND post_name NOT IN ('" . implode("', '", $slugs) . "') AND post_name NOT LIKE 'media%' AND post_name NOT LIKE 'plugins%' AND post_name NOT LIKE 'themes%'";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $id = $row['ID'];
            $sql = "DELETE FROM wp_postmeta WHERE post_id = '$id'";
            mysqli_query($conn, $sql);
            $sql = "DELETE FROM wp_posts WHERE ID = '$id'";
            mysqli_query($conn, $sql);
            echo "Deleted page with slug: " . $row['post_name'] . "\n";
        }
    } else {
        echo "No pages found for deletion.\n";
    }

    // Close database connection
    mysqli_close($conn);
    ?>

Regards

6
  • 1
    What code did you use to remove pages? Is that code still on your site? Mar 14 at 9:25
  • The first few errors mean you're passing a null post into wp_transition_post_status or its hook. At first glance I can't see how you'd manage that without other warnings first.
    – Rup
    Mar 14 at 9:32
  • @Jacob, the code is added to the post. thank you for your eeply. Mar 14 at 10:46
  • @rob, I really have no idea about any of these as i didn't play with wordpress core tables at all. Mar 14 at 10:47
  • I've had the same issue on a website, for me the problem was that the tables were missing the primary key (probably an error generated during a migration)
    – LS_
    Mar 16 at 10:55

1 Answer 1

0

Thanks to _LS for the hint,

In fact, there were many rows with the same ID , i had to create new table , copy the old table rows with auto increment option, remove the old table then rename the new table to the old table's name. that did the trick.

Thanks all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.