Home>
I would like to periodically insert

wordpress's own tabletestintowp_poststable.
Execute SQL statement directly from phpMyadmin's SQL item

INSERT IGNORE INTO wp_posts (post_date, post_title, code, product, count)
SELECT intime, title, code, product, count
FROM test
I can execute it with the SQL statement of

, but I want to do this with cron,
I'm thinking of using$wpdbin PHP, but I'm bothered by errors
If i know someone, I would like to teach you m (__) m

Error message
PHP Fatal error: Uncaught Error: Call to a member function query () on null in `directory path`: 5
Stack trace:
# 0 {main}
  thrown in `directory path` on line 5
Applicable source code
#!/usr/local/php/7.1/bin/php
<? php
global $wpdb;
$wpdb->query ("INSERT IGNORE INTO {$wpdb->posts} (post_date, post_title, code, product, count)
SELECT intime, title, code, product, count
FROM {$wpdb->test} ");
$wpdb->print_error ();
?>
CreateTable

CREATE TABLEwp_posts(
IDbigint (20) unsigned NOT NULL AUTO_INCREMENT,
post_datedatetime NOT NULL DEFAULT '0000-00-00 00:00:00',
post_titletext COLLATE utf8mb4_unicode_520_ci NOT NULL,
post_statusvarchar (20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'publish',
post_namevarchar (200) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
post_typevarchar (20) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'post',
productvarchar (255) DEFAULT NULL,
codevarchar (255) NOT NULL,
countint (11) DEFAULT NULL,
PRIMARY KEY (ID),
UNIQUE KEYcode(code),
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1;


CREATE TABLEtest(
idint (11) NOT NULL AUTO_INCREMENT,
titlevarchar (255) DEFAULT NULL,
productvarchar (255) DEFAULT NULL,
codevarchar (255) NOT NULL,
countint (11) DEFAULT NULL,
intimedatetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
UNIQUE KEYcode(code),
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1;

  • Answer # 1

    $wpdb is only in Global when WordPress is running, so it is not defined even if you execute the PHP file directly.
    I think that either WP-Cron will be used to solve the problem in WordPress, or PHP that directly accesses the DB will be written ($wpdb and other WordPress-derived ones are not used)

    【Perform regular processing with WordPress! How to set up WP-Cron – Tokyo homepage production/web production company BRISK】