{"id":4324,"date":"2025-07-31T13:20:32","date_gmt":"2025-07-31T05:20:32","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4324.html"},"modified":"2025-12-11T10:32:32","modified_gmt":"2025-12-11T02:32:32","slug":"wordpress%e8%8e%b7%e5%8f%96%e4%bb%8a%e6%97%a5%e4%b9%8b%e5%89%8d%e6%9c%80%e5%90%8e%e5%8f%91%e5%b8%83%e7%9a%84%e6%96%87%e7%ab%a0id%e5%ae%9e%e7%8e%b0%e4%bb%a3%e7%a0%81%e7%a4%ba%e4%be%8b","status":"publish","type":"bjjc","link":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4324.html","title":{"rendered":"WordPress\u83b7\u53d6\u4eca\u65e5\u4e4b\u524d\u6700\u540e\u53d1\u5e03\u7684\u6587\u7ae0ID\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b"},"content":{"rendered":"<p>WordPress\u83b7\u53d6\u4eca\u65e5\u4e4b\u524d\u6700\u540e\u53d1\u5e03\u7684\u6587\u7ae0ID\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b<\/p><h2 class=\"wp-block-heading\">\u4ee3\u7801<\/h2><pre class=\"wp-block-code\"><code>\n\/\/\u7528\u4e8e\u66f4\u65b0\u5f53\u5929\u6587\u7ae0\uff1a\u83b7\u53d6\u4eca\u65e5\u4e4b\u524d\u6700\u540e\u53d1\u5e03\u7684\u6587\u7ae0ID\u7684\u5c01\u88c5\u51fd\u6570\n\/\/\u8fd9\u91cc\u662f\u83b7\u53d6\u6cd5\u51fd\u6570\nfunction zzw_get_today_start_post_id() {\n\n    global $wpdb;\n    \n    \/\/ \u83b7\u53d6\u7ad9\u70b9\u65f6\u533a\u7684\u4eca\u592900:00:00\u65f6\u95f4\n    $today_start = current_time('Y-m-d 00:00:00');\n    \n    \/\/ \u7cbe\u786e\u83b7\u53d6\u6240\u6709\u516c\u5f00\u6587\u7ae0\u7c7b\u578b\uff08\u5305\u62ecpost\/page\/\u81ea\u5b9a\u4e49\u7c7b\u578b\uff09\n    $post_types = array_merge(\n        ['post', 'page'],\n        get_post_types([\n            'public'   =&gt; true,\n            '_builtin' =&gt; false\n        ])\n    );\n    \n    \/\/ \u5b89\u5168\u5904\u7406\u6587\u7ae0\u7c7b\u578b\n    $post_types_placeholder = implode(',', array_fill(0, count($post_types), '%s'));\n    \n    \/\/ \u9ad8\u6027\u80fdSQL\u67e5\u8be2\uff08\u4f7f\u7528\u7d22\u5f15\u4f18\u5316\uff09\n    $query = $wpdb-&gt;prepare(\n        \"SELECT ID \n        FROM {$wpdb-&gt;posts} \n        WHERE post_status = 'publish'\n          AND post_type IN ({$post_types_placeholder})\n          AND post_date &lt; %s\n        ORDER BY post_date DESC \n        LIMIT 1\",\n        array_merge($post_types, [$today_start])\n    );\n    \n    $last_post_id = (int) $wpdb-&gt;get_var($query);\n    \n    return $last_post_id;\n\n}\n\n\/\/\u8fd9\u91cc\u662f\u5ef6\u4f38\uff1a\u8d4b\u503c\u7ed9 diy name \u5b57\u6bb5\nfunction zzw_update_today_startpostid()\n{\n    $last_post_id = (string)zzw_get_today_start_post_id();\n    update_option( 'diy name', $last_post_id );\n}\n<\/code><\/pre><p>\u4ee3\u7801\u4f7f\u7528\uff1a<\/p><p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u4f7f\u7528\u91cd\u8981\u4e8b\u9879\uff1a\u5c06 diy name \u8be5\u4e3a\u4f60\u7684\u81ea\u5b9a\u4e49\u5b57\u6bb5\u540d\u79f0<\/mark><\/strong><\/p><p>\u5728\u4f60\u9700\u8981\u6267\u884c\u7684\u4f4d\u7f6e\u6dfb\u52a0 <code>zzw_update_today_startpostid()<\/code> \u5373\u53ef\u3002<\/p><h2 class=\"wp-block-heading\">\u8be6\u89e3<\/h2><p>\u8fd9\u6bb5\u4ee3\u7801\u5305\u542b\u4e24\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u83b7\u53d6\u5e76\u5b58\u50a8\u4eca\u65e5\u4e4b\u524d\u6700\u540e\u53d1\u5e03\u7684\u6587\u7ae0ID\u3002\u4e0b\u9762\u662f\u5bf9\u4ee3\u7801\u7684\u8be6\u7ec6\u89e3\u6790\uff1a<\/p><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>\u51fd\u6570\u4e00\uff1a<code>zzw_get_today_start_post_id()<\/code><\/strong><\/h3><p><strong>\u4f5c\u7528<\/strong>\uff1a\u9ad8\u6548\u67e5\u8be2\u4eca\u65e500:00:00\u4e4b\u524d\u53d1\u5e03\u7684\u6700\u540e\u4e00\u7bc7\u6587\u7ae0\u7684ID\uff08\u652f\u6301\u6240\u6709\u516c\u5f00\u6587\u7ae0\u7c7b\u578b\uff09\u3002<\/p><h4 class=\"wp-block-heading\">\u4ee3\u7801\u89e3\u6790\uff1a<\/h4><ol class=\"wp-block-list\">\n<li><strong>\u5168\u5c40\u6570\u636e\u5e93\u5bf9\u8c61<\/strong><\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   global $wpdb;<\/code><\/pre><p>\u4f7f\u7528WordPress\u7684\u6570\u636e\u5e93\u5bf9\u8c61\u8fdb\u884c\u67e5\u8be2\u3002<\/p><ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u83b7\u53d6\u5f53\u5929\u8d77\u59cb\u65f6\u95f4<\/strong><\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   $today_start = current_time('Y-m-d 00:00:00');<\/code><\/pre><ul class=\"wp-block-list\">\n<li><code>current_time()<\/code> \u6839\u636eWordPress\u7ad9\u70b9\u65f6\u533a\u8fd4\u56de\u5f53\u524d\u65f6\u95f4<\/li>\n\n\n\n<li>\u683c\u5f0f\u5316\u4e3a\u5f53\u5929\u768400:00:00\uff08\u5982 <code>2025-07-31 00:00:00<\/code>\uff09<\/li>\n<\/ul><ol class=\"wp-block-list\">\n<li><strong>\u83b7\u53d6\u6240\u6709\u516c\u5f00\u6587\u7ae0\u7c7b\u578b<\/strong><\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   $post_types = array_merge(\n       ['post', 'page'],\n       get_post_types([\n           'public'   =&gt; true,\n           '_builtin' =&gt; false\n       ])\n   );<\/code><\/pre><ul class=\"wp-block-list\">\n<li>\u5305\u542b\u9ed8\u8ba4\u6587\u7ae0\u7c7b\u578b <code>post<\/code> \u548c <code>page<\/code><\/li>\n\n\n\n<li>\u52a8\u6001\u83b7\u53d6\u6240\u6709\u81ea\u5b9a\u4e49\u516c\u5f00\u6587\u7ae0\u7c7b\u578b\uff08\u5982\u4ea7\u54c1\u3001\u9879\u76ee\u7b49\uff09<\/li>\n<\/ul><ol class=\"wp-block-list\">\n<li><strong>\u6784\u5efa\u5b89\u5168\u7684SQL\u5360\u4f4d\u7b26<\/strong><\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   $post_types_placeholder = implode(',', array_fill(0, count($post_types), '%s'));<\/code><\/pre><p>\u751f\u6210\u4e0e\u6587\u7ae0\u7c7b\u578b\u6570\u91cf\u5339\u914d\u7684\u5360\u4f4d\u7b26\uff08\u5982 <code>'%s','%s','%s'<\/code>\uff09\uff0c\u9632\u6b62SQL\u6ce8\u5165\u3002<\/p><ol start=\"5\" class=\"wp-block-list\">\n<li><strong>\u6267\u884c\u4f18\u5316\u67e5\u8be2<\/strong><\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   $query = $wpdb-&gt;prepare(\n       \"SELECT ID \n        FROM {$wpdb-&gt;posts} \n        WHERE post_status = 'publish'\n          AND post_type IN ({$post_types_placeholder})\n          AND post_date &lt; %s\n        ORDER BY post_date DESC \n        LIMIT 1\",\n       array_merge($post_types, [$today_start])\n   );<\/code><\/pre><ul class=\"wp-block-list\">\n<li><strong>\u6761\u4ef6<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6587\u7ae0\u72b6\u6001\u4e3a\u5df2\u53d1\u5e03 (<code>publish<\/code>)<\/li>\n\n\n\n<li>\u6587\u7ae0\u7c7b\u578b\u5728\u6307\u5b9a\u8303\u56f4\u5185<\/li>\n\n\n\n<li>\u53d1\u5e03\u65f6\u95f4\u65e9\u4e8e\u4eca\u65e500:00:00<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6392\u5e8f<\/strong>\uff1a\u6309\u53d1\u5e03\u65f6\u95f4\u5012\u5e8f\uff08\u83b7\u53d6\u6700\u65b0\u7684\u4e00\u7bc7\uff09<\/li>\n\n\n\n<li><strong>\u9650\u5236<\/strong>\uff1a\u4ec5\u8fd4\u56de1\u6761\u7ed3\u679c<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u4f7f\u7528 <code>prepare()<\/code> \u52a8\u6001\u7ed1\u5b9a\u53c2\u6570<\/li>\n<\/ul><ol class=\"wp-block-list\">\n<li><strong>\u8fd4\u56de\u7ed3\u679c<\/strong><\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   $last_post_id = (int) $wpdb-&gt;get_var($query);\n   return $last_post_id;<\/code><\/pre><p>\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u4e3a\u6574\u6570\u5e76\u8fd4\u56de\uff08\u65e0\u7ed3\u679c\u65f6\u8fd4\u56de <code>0<\/code>\uff09<\/p><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>\u51fd\u6570\u4e8c\uff1a<code>zzw_update_today_startpostid()<\/code><\/strong><\/h3><p><strong>\u4f5c\u7528<\/strong>\uff1a\u5c06\u83b7\u53d6\u7684\u6587\u7ae0ID\u5b58\u50a8\u5230WordPress\u9009\u9879\u8868\u4e2d\u3002<\/p><h4 class=\"wp-block-heading\">\u4ee3\u7801\u89e3\u6790\uff1a<\/h4><pre class=\"wp-block-code\"><code>function zzw_update_today_startpostid() {\n    $last_post_id = (string)zzw_get_today_start_post_id();\n    update_option( 'diy name', $last_post_id );\n}<\/code><\/pre><ol class=\"wp-block-list\">\n<li>\u8c03\u7528 <code>zzw_get_today_start_post_id()<\/code> \u83b7\u53d6\u6587\u7ae0ID<\/li>\n\n\n\n<li>\u5c06ID\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff08WordPress\u9009\u9879\u503c\u901a\u5e38\u6309\u5b57\u7b26\u4e32\u5b58\u50a8\uff09<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>update_option()<\/code> \u5c06\u503c\u4fdd\u5b58\u5230\u540d\u4e3a <code>diy name<\/code> \u7684\u9009\u9879\u4e2d\uff08\u81ea\u52a8\u521b\u5efa\/\u66f4\u65b0\uff09<\/li>\n<\/ol><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>\u5173\u952e\u7279\u6027\u603b\u7ed3<\/strong><\/h3><figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><strong>\u65f6\u533a\u5904\u7406<\/strong><\/td><td>\u4f7f\u7528 <code>current_time()<\/code> \u786e\u4fdd\u7b26\u5408\u7ad9\u70b9\u65f6\u533a\u8bbe\u7f6e<\/td><\/tr><tr><td><strong>\u5168\u9762\u6587\u7ae0\u7c7b\u578b<\/strong><\/td><td>\u8986\u76d6\u6240\u6709\u516c\u5f00\u7c7b\u578b\uff08\u5185\u7f6e+\u81ea\u5b9a\u4e49\uff09<\/td><\/tr><tr><td><strong>\u9ad8\u6027\u80fd\u67e5\u8be2<\/strong><\/td><td>\u901a\u8fc7 <code>ORDER BY post_date DESC + LIMIT 1<\/code> \u5229\u7528\u6570\u636e\u5e93\u7d22\u5f15\u4f18\u5316<\/td><\/tr><tr><td><strong>\u9632SQL\u6ce8\u5165<\/strong><\/td><td><code>prepare()<\/code> + \u53c2\u6570\u7ed1\u5b9a\u786e\u4fdd\u67e5\u8be2\u5b89\u5168<\/td><\/tr><tr><td><strong>\u6570\u636e\u5b58\u50a8<\/strong><\/td><td>\u7ed3\u679c\u6301\u4e45\u5316\u5230\u9009\u9879\u8868\uff0c\u5168\u5c40\u53ef\u7528<\/td><\/tr><\/tbody><\/table><\/figure><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>\u4f7f\u7528\u573a\u666f\u793a\u4f8b<\/strong><\/h3><p>\u5047\u8bbe\u9700\u8981\u5728\u9996\u9875\u663e\u793a&#8221;\u6628\u65e5\u6700\u65b0\u6587\u7ae0&#8221;\uff1a<\/p><pre class=\"wp-block-code\"><code>$yesterdays_last_id = get_option('diy name');\nif ($yesterdays_last_id) {\n    $post = get_post($yesterdays_last_id);\n    echo '\u6628\u65e5\u5934\u6761\uff1a' . $post-&gt;post_title;\n}<\/code><\/pre><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>\u4f18\u5316\u5efa\u8bae<\/strong><\/h3><ol class=\"wp-block-list\">\n<li><strong>\u9009\u9879\u547d\u540d<\/strong><br>\u5c06 <code>'diy name'<\/code> \u6539\u4e3a\u66f4\u5177\u63cf\u8ff0\u6027\u7684\u540d\u79f0\uff08\u5982 <code>'zzw_last_pre_today_post_id'<\/code>\uff09\uff0c\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong><br>\u6dfb\u52a0\u67e5\u8be2\u7ed3\u679c\u9a8c\u8bc1\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   if (false === $last_post_id) {\n       \/\/ \u8bb0\u5f55\u6570\u636e\u5e93\u9519\u8bef\n   }<\/code><\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u7f13\u5b58\u673a\u5236<\/strong><br>\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u7f13\u5b58\uff08\u5982\u4f7f\u7528 <code>wp_cache<\/code>\uff09\uff0c\u51cf\u5c11\u6570\u636e\u5e93\u8bf7\u6c42\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   $cache_key = 'zzw_today_start_post_id';\n   if (false === ($id = wp_cache_get($cache_key))) {\n       $id = zzw_get_today_start_post_id();\n       wp_cache_set($cache_key, $id, '', 12 * HOUR_IN_SECONDS);\n   }\n   return $id;<\/code><\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li><strong>\u5b9a\u65f6\u4efb\u52a1<\/strong><br>\u7ed3\u5408WordPress Cron \u6bcf\u65e5\u81ea\u52a8\u66f4\u65b0\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   add_action('zzw_daily_update', 'zzw_update_today_startpostid');\n   \/\/ \u5728\u4e3b\u9898\/\u63d2\u4ef6\u6fc0\u6d3b\u65f6\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\n   if (!wp_next_scheduled('zzw_daily_update')) {\n       wp_schedule_event(strtotime('tomorrow 00:05'), 'daily', 'zzw_daily_update');\n   }<\/code><\/pre><p>\u8fd9\u6bb5\u4ee3\u7801\u901a\u8fc7\u4e25\u8c28\u7684\u53c2\u6570\u5904\u7406\u548c\u9ad8\u6548\u7684SQL\u67e5\u8be2\uff0c\u5b9e\u73b0\u4e86\u8de8\u6587\u7ae0\u7c7b\u578b\u7684&#8221;\u4eca\u65e5\u524d\u6700\u65b0\u6587\u7ae0ID&#8221;\u83b7\u53d6\u4e0e\u5b58\u50a8\u529f\u80fd\uff0c\u9002\u5408\u9700\u8981\u65f6\u95f4\u8fb9\u754c\u6587\u7ae0\u67e5\u8be2\u7684\u573a\u666f\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>WordPress\u83b7\u53d6\u4eca\u65e5\u4e4b\u524d\u6700\u540e\u53d1\u5e03\u7684\u6587\u7ae0ID\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b \u4ee3\u7801 \u4ee3\u7801\u4f7f\u7528\uff1a \u4f7f\u7528\u91cd\u8981\u4e8b\u9879\uff1a\u5c06 diy name \u8be5\u4e3a\u4f60\u7684\u81ea\u5b9a\u4e49\u5b57\u6bb5\u540d\u79f0 \u5728\u4f60\u9700\u8981\u6267\u884c\u7684\u4f4d\u7f6e\u6dfb\u52a0 zzw_update_today_startpostid() \u5373\u53ef\u3002 \u8be6\u89e3 \u8fd9\u6bb5\u4ee3\u7801\u5305\u542b\u4e24\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u83b7\u53d6\u5e76\u5b58\u50a8\u4eca\u65e5\u4e4b\u524d\u6700\u540e\u53d1\u5e03\u7684\u6587\u7ae0ID\u3002\u4e0b\u9762\u662f\u5bf9\u4ee3\u7801\u7684\u8be6..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[3222],"bjjc2nav":[34],"tuisongtax":[],"class_list":["post-4324","bjjc","type-bjjc","status-publish","hentry","tag-hqtdwzdff","bjjc2nav-wptspz"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"\u83b7\u53d6\u4eca\u65e5\u4e4b\u524d\u6700\u540e\u53d1\u5e03\u7684\u6587\u7ae0ID","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":"\u83b7\u53d6\u4eca\u65e5\u4e4b\u524d\u6700\u540e\u53d1\u5e03\u7684\u6587\u7ae0ID"}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc\/4324","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=4324"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tags?post=4324"},{"taxonomy":"bjjc2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc2nav?post=4324"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tuisongtax?post=4324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}