1

I have an external script that does some DB maintenance through WP_Query and similar functions. It used to work great, but now after a PHP version update (from 7 to 8) it stopped working.

The necessary WP header files are included, but the script crashes at first WP function call without an error message. I tried to call various WP functions. What can be the problem?

Here is the script:

<?php
define( 'WP_USE_THEMES', false );

require __DIR__ . '/wp-blog-header.php';

    // The Query
    $args = array(
        'post_type' => 'post',
        "posts_per_page" => "1",
        'order' => 'DESC',
        'orderby' => 'ID'
    );

    $the_query = new WP_Query( $args );
?>

UPDATE: I succeeded to get the error message finally:

Fatal error: Uncaught Error: Class "WP_Query" not found in /home/zrksvcvh/public_html/videos_update.php:46 Stack trace: #0 {main} thrown in /home/[...]/public_html/videos_update.php on line 46

5
  • you haven't included the error message, what does your maintenance script actually maintain? Could this not be done via a WP CLI command or a cron job inside a WP plugin? Commented Feb 2, 2024 at 12:17
  • There is no error message... I wrote above. The script should insert a bunch of new records manually, but this is not relevant, since the process got stuck way earlier. Commented Feb 2, 2024 at 12:30
  • is that the entire script? You mention inserting new records but there is nothing in the code you shared that would do this, are there pieces of code missing from your script? If you've tried to simplify the code to cut out distractions you might have removed the cause of the problem too, especially if it's related to PHP versions. When you say that it crashes/gets stuck, can you be more specific? Timeouts? Script runs forever? Script runs but the posts you expected to be inserted are not inserted? Be as specific as you can Commented Feb 2, 2024 at 14:50
  • Updated. Include didn't succeed. I tried various forms, but it always gets this error message. Commented Feb 4, 2024 at 13:23
  • 2
    have you considered putting this code inside something other than a standalone PHP file that tries to load WP? A WP Cron job would be much safer and avoid this issue entirely, right now if I figured out any of the sites you've worked on I could write a 1 line snippet of code to repeatedly hit /videos_update.php and bring your server down Commented Feb 4, 2024 at 13:34

1 Answer 1

1

The problem is that you're trying to do your work in a stand alone PHP, this will always be fragile, and it poses a security risk!

Use a Cron Job Instead

This will trigger your code to run hourly:

add_action( 'init', function() {
    // If the cron job hasn't been scheduled
    if ( ! wp_next_scheduled ( 'binyomins_cron_job' )) {
        // schedule it to fire an event every hour
        wp_schedule_event( time(), 'hourly', 'binyomins_cron_job' );
    }

    // Run the video update function every time the cron event happens.
    add_action( 'binyomins_cron_job', 'binyomin_video_update' );
});

function binyomin_video_update(): void {
    // .... your code goes here
    // The Query
    $args = array(
        'post_type' => 'post',
        "posts_per_page" => 1,
....... etc
}

You can change it to run daily, or even register your own schedule.

You could also swap out the scheduled cron with a single event if you chose, running it via cron tools such as WP CLI or the WP Crontrol plugin.

Where Does That Code Go?

Anywhere hooks and filters usually go, a plugin, a functions.php, etc

2
  • Thank you! Indeed this was the problem. The solution was to create a new simple plugin with the add_posts_page() function to run it from the dashboard manually. Commented Feb 5, 2024 at 17:46
  • A WP CLI command would also have worked for a manual run option. A REST API endpoint also works but would be more complicated to trigger securely than the other options Commented Feb 5, 2024 at 17:50

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.