{"id":4221,"date":"2025-07-09T19:05:58","date_gmt":"2025-07-09T11:05:58","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4221.html"},"modified":"2025-12-11T10:22:00","modified_gmt":"2025-12-11T02:22:00","slug":"wordpress%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%e5%b8%b8%e7%94%a8%e8%af%ad%e5%8f%a5%e4%bb%a3%e7%a0%81%e5%8f%8a%e6%b3%a8%e6%84%8f%e8%a6%81%e7%82%b9","status":"publish","type":"bjjc","link":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4221.html","title":{"rendered":"WordPress\u6570\u636e\u5e93\u64cd\u4f5c\u5e38\u7528\u8bed\u53e5\u4ee3\u7801\u96c6\u53ca\u6ce8\u610f\u8981\u70b9"},"content":{"rendered":"<h2 class=\"wp-block-heading\">\u5b89\u5168\u83b7\u53d6\u6570\u636e\u8868\uff1a\u8868\u524d\u7f00\u5904\u7406<\/h2><pre class=\"wp-block-code\"><code>$table_name = $wpdb-&gt;prefix . TABLE_NAME;\n\/\/TABLE_NAME\uff1a\u5982\u201c_options\u201d\u3001\u201c_posts\u201d\u3001\u201c_terms\u201d..<\/code><\/pre><p>\u8868\u524d\u7f00\u5904\u7406\uff1a\u81ea\u52a8\u9002\u914d<code>wp_<\/code>\u3001<code>wp_2_<\/code>\u7b49\u7ad9\u70b9\u524d\u7f00<\/p><h2 class=\"wp-block-heading\">\u5b89\u5168\u5220\u9664\u6570\u636e\u8868<\/h2><pre class=\"wp-block-code\"><code>$wpdb-&gt;query(\"DROP TABLE IF EXISTS $table_name\");<\/code><\/pre><p>\u4f7f\u7528<code>IF EXISTS<\/code>\u907f\u514d\u4e0d\u5b58\u5728\u7684\u8868\u5bfc\u81f4\u9519\u8bef<\/p><h2 class=\"wp-block-heading\">\u6570\u636e\u8868\u5b58\u5728\u6027\u68c0\u67e5<\/h2><pre class=\"wp-block-code\"><code>if ($wpdb-&gt;get_var(\"SHOW TABLES LIKE '$table_name'\") === $table_name) {\n    return;\n}<\/code><\/pre><p>\u9632\u6b62\u91cd\u590d\u521b\u5efa\u5bfc\u81f4\u7684SQL\u9519\u8bef<\/p><h2 class=\"wp-block-heading\">\u5b89\u5168\u521b\u5efa\u6570\u636e\u8868\uff1a\u4f7f\u7528dbDelta\u51fd\u6570<\/h2><pre class=\"wp-block-code\"><code>global $wpdb; \/\/ \u83b7\u53d6WordPress\u6570\u636e\u5e93\u5bf9\u8c61\n    \n\/**\n * \u6784\u5efa\u5b8c\u6574\u8868\u540d\uff1a\n * 1. $wpdb-&gt;prefix \u83b7\u53d6WordPress\u8868\u524d\u7f00\uff08\u5982wp_\uff09\n * 2. \u62fc\u63a5\u5e38\u91cf TABLE_NAME\n *\/\n$table_name = $wpdb-&gt;prefix . TABLE_NAME;\n\n\/**\n * \u68c0\u67e5\u8868\u662f\u5426\u5df2\u5b58\u5728\uff1a\n * \u907f\u514d\u91cd\u590d\u521b\u5efa\u5bfc\u81f4\u9519\u8bef\uff0c\u63d0\u5347\u63d2\u4ef6\u5065\u58ee\u6027\n *\/\nif ($wpdb-&gt;get_var(\"SHOW TABLES LIKE '$table_name'\") === $table_name) {\n    return; \/\/ \u8868\u5df2\u5b58\u5728\uff0c\u65e0\u9700\u521b\u5efa\n}\n\n\/**\n * \u83b7\u53d6\u6570\u636e\u5e93\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\uff1a\n * \u786e\u4fdd\u4e0eWordPress\u6570\u636e\u5e93\u8bbe\u7f6e\u4e00\u81f4\n *\/\n$charset_collate = $wpdb-&gt;get_charset_collate();\n\n\/**\n * SQL\u521b\u5efa\u8bed\u53e5\uff1a\n * 1. id: \u81ea\u589e\u4e3b\u952e\uff0c\u552f\u4e00\u6807\u8bc6\u6bcf\u4e2a\u4efb\u52a1\n * 2. type: \u4efb\u52a1\u7c7b\u578b\uff08\u9996\u9875\/\u5206\u7c7b\/\u6587\u7ae0\uff09\n * 3. object_id: \u5173\u8054\u5bf9\u8c61ID\uff080\u8868\u793a\u9996\u9875\uff09\n * 4. status: \u4efb\u52a1\u72b6\u6001\uff08\u5f85\u5904\u7406\/\u5904\u7406\u4e2d\/\u5b8c\u6210\/\u5931\u8d25\uff09\n * 5. created: \u4efb\u52a1\u521b\u5efa\u65f6\u95f4\n * 6. updated: \u6700\u540e\u66f4\u65b0\u65f6\u95f4\uff08\u81ea\u52a8\u66f4\u65b0\uff09\n * \n * \u7d22\u5f15\u8bf4\u660e\uff1a\n * - PRIMARY KEY (id): \u4e3b\u952e\u7d22\u5f15\uff0c\u5feb\u901f\u5b9a\u4f4d\u4efb\u52a1\n * - KEY type_status (type, status): \u590d\u5408\u7d22\u5f15\uff0c\u4f18\u5316\u6309\u7c7b\u578b\u548c\u72b6\u6001\u7684\u67e5\u8be2\n * - KEY object_id (object_id): \u5355\u5217\u7d22\u5f15\uff0c\u4f18\u5316\u6309\u5bf9\u8c61ID\u7684\u67e5\u8be2\n *\/\n$sql = \"CREATE TABLE $table_name (\n    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '\u4efb\u52a1\u552f\u4e00ID',\n    type varchar(20) NOT NULL COMMENT '\u4efb\u52a1\u7c7b\u578b: home(\u9996\u9875), term(\u5206\u7c7b), post(\u6587\u7ae0)',\n    object_id bigint(20) NOT NULL DEFAULT '0' COMMENT '\u5173\u8054\u5bf9\u8c61ID: \u9996\u9875=0, \u5206\u7c7b=term_id, \u6587\u7ae0=post_id',\n    status varchar(20) NOT NULL DEFAULT 'pending' COMMENT '\u4efb\u52a1\u72b6\u6001: pending(\u5f85\u5904\u7406), processing(\u5904\u7406\u4e2d), completed(\u5b8c\u6210), failed(\u5931\u8d25)',\n    created datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '\u4efb\u52a1\u521b\u5efa\u65f6\u95f4',\n    updated datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '\u6700\u540e\u66f4\u65b0\u65f6\u95f4',\n    PRIMARY KEY (id),\n    KEY type_status (type, status),\n    KEY object_id (object_id)\n) $charset_collate;\";\n\n\/**\n * \u5305\u542bWordPress\u5347\u7ea7API\uff1a\n * dbDelta()\u51fd\u6570\u7528\u4e8e\u5b89\u5168\u6267\u884cSQL\u521b\u5efa\/\u66f4\u65b0\u8868\u7ed3\u6784\n * \u5b83\u4f1a\u667a\u80fd\u6bd4\u8f83\u73b0\u6709\u8868\u7ed3\u6784\u5e76\u6267\u884c\u5fc5\u8981\u66f4\u6539\n *\/\nrequire_once(ABSPATH . 'wp-admin\/includes\/upgrade.php');\n\n\/\/ \u6267\u884c\u8868\u521b\u5efa\/\u66f4\u65b0\ndbDelta($sql);\n<\/code><\/pre><p>WordPress\u6838\u5fc3\u51fd\u6570\uff0c\u667a\u80fd\u5904\u7406\u8868\u7ed3\u6784\u5dee\u5f02<\/p><p>\u53ea\u6267\u884c\u5fc5\u8981\u7684ALTER TABLE\u64cd\u4f5c<\/p><h2 class=\"wp-block-heading\">\u901a\u8fc7\u6587\u7ae0ID\u83b7\u53d6\u5bf9\u5e94\u7684post_type\u540d<\/h2><p>\u6570\u636e\u5e93\u83b7\u53d6\u65b9\u6cd5\uff1a<\/p><pre class=\"wp-block-code\"><code>global $wpdb;\n        $post_type = $wpdb-&gt;get_var( $wpdb-&gt;prepare(\n            \"SELECT post_type FROM $wpdb-&gt;posts WHERE ID = %d\", \n            $post_id\n        ));<\/code><\/pre><h2 class=\"wp-block-heading\">\u901a\u8fc7\u6587\u7ae0ID\u83b7\u53d6\u8be5\u6587\u7ae0\u522b\u540d<\/h2><p>\u6570\u636e\u5e93\u65b9\u6cd5\uff1a<\/p><pre class=\"wp-block-code\"><code>global $wpdb;\n$post_slug = $wpdb-&gt;get_var(\n            $wpdb-&gt;prepare(\"SELECT post_name FROM $wpdb-&gt;posts WHERE ID = %d\", $post_id)\n        );<\/code><\/pre><h2 class=\"wp-block-heading\">\u6570\u636e\u5e93\u65b9\u5f0f\u5206\u6279\u83b7\u53d6\u5f53\u5929\u6587\u7ae0<\/h2><pre class=\"wp-block-code\"><code>\/\/ \u4f7f\u7528\u6b63\u786e\u7684\u65f6\u533a\u5904\u7406\u65b9\u5f0f\n$timezone = get_option('timezone_string');\nif ($timezone) date_default_timezone_set($timezone);\n\n$today_start = date('Y-m-d 00:00:00');\n$today_end   = date('Y-m-d 23:59:59');\n\n$last_id = 0;\/\/ \u83b7\u53d6\u8d77\u59cb\u7684\u6587\u7ae0ID\n\n\/\/ \u5206\u6279:\u5165\u5217\u5230\u6570\u636e\u5e93\u7684\u961f\u5217\u6570\u636e\u8868------------------------------------------------------\n$batch_size = 1000;\/\/\u5355\u6279\u83b7\u53d6\u6570\u91cf\n$placeholders = implode(', ', array_fill(0, count($post_types), '%s'));\n\n\/\/\u83b7\u53d6\uff1a\u67e5\u8be2\u5b57\u6bb5ID, post_type, post_name\n$sql = $wpdb-&gt;prepare(\n    \"SELECT ID, post_type, post_name FROM $wpdb-&gt;posts\n     WHERE post_type IN ($placeholders)\n       AND post_status = 'publish'\n       AND CAST(post_date AS DATE) BETWEEN %s AND %s\n       AND ID &gt; %d\n     ORDER BY ID ASC\n     LIMIT %d\",\n    array_merge(\n        $post_types,\n        [$today_start, $today_end, $last_id, $batch_size]\n    )\n);\n\n$results = $wpdb-&gt;get_results($sql);\n\/* \u83b7\u53d6\u7ed3\u679c\u793a\u4f8b\uff1a\nArray\n(\n    [0] =&gt; stdClass Object\n        (\n            [ID] =&gt; 3200\n            [post_type] =&gt; 'post'\n            [post_name] =&gt; 'hello-world'\n        )\n    [1] =&gt; stdClass Object\n        (\n            [ID] =&gt; 3202\n            [post_type] =&gt; 'page'\n            [post_name] =&gt; 'about-us'\n        )\n    [2] =&gt; stdClass Object\n        (\n            [ID] =&gt; 3204\n            [post_type] =&gt; 'product'\n            [post_name] =&gt; 'tshirt-2023'\n        )\n)\n*\/\n\nif (!empty($results)) {\n    foreach ($results as $row) {\n        \/\/\u4f60\u7684\u64cd\u4f5c\n    }\n}\n\n\/\/ \u6062\u590d\u539f\u59cb\u65f6\u533a\nif ($timezone) date_default_timezone_set('UTC');<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>\u5b89\u5168\u83b7\u53d6\u6570\u636e\u8868\uff1a\u8868\u524d\u7f00\u5904\u7406 \u8868\u524d\u7f00\u5904\u7406\uff1a\u81ea\u52a8\u9002\u914dwp_\u3001wp_2_\u7b49\u7ad9\u70b9\u524d\u7f00 \u5b89\u5168\u5220\u9664\u6570\u636e\u8868 \u4f7f\u7528IF EXISTS\u907f\u514d\u4e0d\u5b58\u5728\u7684\u8868\u5bfc\u81f4\u9519\u8bef \u6570\u636e\u8868\u5b58\u5728\u6027\u68c0\u67e5 \u9632\u6b62\u91cd\u590d\u521b\u5efa\u5bfc\u81f4\u7684SQL\u9519\u8bef \u5b89\u5168\u521b\u5efa\u6570\u636e\u8868\uff1a\u4f7f\u7528dbDelta\u51fd\u6570 WordPress\u6838\u5fc3\u51fd\u6570\uff0c\u667a\u80fd\u5904\u7406\u8868\u7ed3\u6784\u5dee\u5f02 \u53ea\u6267\u884c\u5fc5\u8981\u7684ALTER TABLE\u64cd\u4f5c \u901a\u8fc7..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":true},"tags":[3201],"bjjc2nav":[3217],"tuisongtax":[],"class_list":["post-4221","bjjc","type-bjjc","status-publish","hentry","tag-sjkcz","bjjc2nav-datatable"],"acf":{"qian_art_seotitle":"WordPress\u6570\u636e\u5e93\u64cd\u4f5c\u5e38\u7528\u8bed\u53e5\u4ee3\u7801\u96c6\u53ca\u6ce8\u610f\u8981\u70b9","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":"WordPress\u6570\u636e\u5e93\u64cd\u4f5c\u5e38\u7528\u8bed\u53e5\u4ee3\u7801\u96c6\u53ca\u6ce8\u610f\u8981\u70b9"},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"\u6570\u636e\u5e93\uff1a\u5e38\u7528\u8bed\u53e5\u4ee3\u7801\u96c6","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":"\u6570\u636e\u5e93\uff1a\u5e38\u7528\u8bed\u53e5\u4ee3\u7801\u96c6"}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc\/4221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/types\/bjjc"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/media?parent=4221"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tags?post=4221"},{"taxonomy":"bjjc2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc2nav?post=4221"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tuisongtax?post=4221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}