{"id":4210,"date":"2025-06-27T15:11:13","date_gmt":"2025-06-27T07:11:13","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4210.html"},"modified":"2025-12-11T10:18:21","modified_gmt":"2025-12-11T02:18:21","slug":"wordpress%e8%8e%b7%e5%8f%96%e6%96%87%e7%ab%a0%e6%80%bb%e6%95%b0%e9%87%8f%e7%9a%84%e6%96%b9%e6%b3%95%ef%bc%8c%e7%99%be%e4%b8%87%e5%8d%83%e4%b8%87%e7%ba%a7%e6%95%b0%e6%8d%ae%e4%bc%98%e5%8c%96%e6%96%b9","status":"publish","type":"bjjc","link":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4210.html","title":{"rendered":"WordPress\u83b7\u53d6\u6587\u7ae0\u603b\u6570\u91cf\u7684\u65b9\u6cd5\uff0c\u767e\u4e07\u5343\u4e07\u7ea7\u6570\u636e\u4f18\u5316\u65b9\u6848"},"content":{"rendered":"<p>\u5728WordPress\u5927\u578b\u7f51\u7ad9\u4e2d\uff0c\u83b7\u53d6<code>post<\/code>\u548c<code>chan<\/code>\u4e24\u79cd\u6587\u7ae0\u7c7b\u578b\u7684\u5168\u90e8\u6587\u7ae0\u6570\u91cf\uff08\u9ed8\u8ba4\u7edf\u8ba1\u5df2\u53d1\u5e03\u72b6\u6001\uff09\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u9ad8\u6548\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p><h2 class=\"wp-block-heading\">\u63a8\u8350\u65b9\u6cd5\uff1a<code>wp_count_posts()<\/code>\u51fd\u6570<\/h2><p><strong>\u539f\u7406<\/strong>\uff1a\u76f4\u63a5\u8c03\u7528WordPress\u6838\u5fc3\u51fd\u6570\uff0c\u4ece\u6570\u636e\u5e93\u7f13\u5b58\u4e2d\u8bfb\u53d6\u7edf\u8ba1\u503c\uff0c\u6027\u80fd\u6700\u4f18\uff08\u65f6\u95f4\u590d\u6742\u5ea6 <em>O(1)<\/em>\uff09\u3002<\/p><p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong>\uff1a <\/p><pre class=\"wp-block-code\"><code><code>\/\/ \u83b7\u53d6 'post' \u7c7b\u578b\u7684\u53d1\u5e03\u72b6\u6001\u6587\u7ae0\u6570\u91cf <\/code><code>$post_count = wp_count_posts('post')-&gt;publish; <\/code>\n<code>\/\/ \u83b7\u53d6 'chan' \u7c7b\u578b\u7684\u53d1\u5e03\u72b6\u6001\u6587\u7ae0\u6570\u91cf <\/code><code>$chan_count = wp_count_posts('chan')-&gt;publish; \/\/ \u8ba1\u7b97\u603b\u6570 $total = $post_count + $chan_count; echo \"\u603b\u6587\u7ae0\u6570\u91cf: \" . $total;<\/code><\/code><\/pre><p><strong>\u4f18\u52bf<\/strong>\uff1a<\/p><p>\u65e0\u9700\u52a0\u8f7d\u6587\u7ae0\u5bf9\u8c61\uff0c\u5185\u5b58\u5360\u7528\u6781\u4f4e\u3002<\/p><p>\u652f\u6301\u6240\u6709\u6ce8\u518c\u7684\u6587\u7ae0\u72b6\u6001\uff08\u5982 <code>draft<\/code>, <code>pending<\/code>, <code>private<\/code>\uff09\uff0c\u66ff\u6362 <code>-&gt;publish<\/code> \u5373\u53ef\u5207\u6362\u72b6\u6001\u3002<\/p><h2 class=\"wp-block-heading\"><strong>\u66ff\u4ee3\u65b9\u6cd5\uff1a<code>WP_Query<\/code>\uff08\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\uff09<\/strong><\/h2><p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong>\uff1a <\/p><pre class=\"wp-block-code\"><code>$query_post = new WP_Query(array(\n    'post_type'      =&gt; 'post',\n    'post_status'    =&gt; 'publish',\n    'posts_per_page' =&gt; -1,\n    'fields'         =&gt; 'ids' \/\/ \u4ec5\u83b7\u53d6ID\u4ee5\u51cf\u5c11\u5185\u5b58\n));\n$post_count = $query_post-&gt;found_posts;\n\n$query_chan = new WP_Query(array(\n    'post_type'      =&gt; 'chan',\n    'post_status'    =&gt; 'publish',\n    'posts_per_page' =&gt; -1,\n    'fields'         =&gt; 'ids'\n));\n$chan_count = $query_chan-&gt;found_posts;\n\n$total = $post_count + $chan_count;<\/code><\/pre><p><strong>\u6ce8\u610f<\/strong>\uff1a<br>\u5728\u6587\u7ae0\u91cf\u8fc7\u5927\uff08&gt;10,000\u7bc7\uff09\u65f6\u53ef\u80fd\u5f15\u53d1\u6027\u80fd\u95ee\u9898\uff0c\u56e0\u9700\u904d\u5386\u6240\u6709\u6587\u7ae0\u3002<\/p><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h2 class=\"wp-block-heading\">\u26a0\ufe0f \u5173\u952e\u6ce8\u610f\u4e8b\u9879<\/h2><p><strong>\u81ea\u5b9a\u4e49\u7c7b\u578b\u6ce8\u518c\u9a8c\u8bc1<\/strong>\uff1a<br>\u786e\u4fdd <code>chan<\/code> \u5df2\u901a\u8fc7 <code>register_post_type()<\/code> \u6b63\u786e\u6ce8\u518c\uff0c\u5426\u5219 <code>wp_count_posts()<\/code> \u8fd4\u56de\u7a7a\u503c\u3002<\/p><p><strong>\u72b6\u6001\u6269\u5c55<\/strong>\uff1a<br>\u82e5\u9700\u7edf\u8ba1\u8349\u7a3f\u3001\u5b9a\u65f6\u6587\u7ae0\u7b49\uff0c\u4f7f\u7528 <code>wp_count_posts('chan')-&gt;draft<\/code> \u6216\u904d\u5386\u72b6\u6001\u6c42\u548c\u3002<\/p><p><strong>\u5927\u578b\u7ad9\u70b9\u4f18\u5316<\/strong>\uff1a<\/p><p>\u4f7f\u7528 <code>wp_cache_set()<\/code> \u7f13\u5b58\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u67e5\u8be2\u3002<\/p><p>\u76f4\u63a5SQL\u67e5\u8be2\uff08\u4e0d\u63a8\u8350\uff09\uff1a<\/p><pre class=\"wp-block-code\"><code><code>sql SELECT COUNT(*) FROM wp_posts WHERE post_type IN ('post', 'chan') AND post_status = 'publish';<\/code><\/code><\/pre><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h2 class=\"wp-block-heading\">\u6027\u80fd\u5bf9\u6bd4\u8868<\/h2><figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th><th>\u5185\u5b58\u5360\u7528<\/th><th>\u9002\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td><code>wp_count_posts()<\/code><\/td><td><em>O(1)<\/em><\/td><td>\u6781\u4f4e<\/td><td>\u5927\u578b\u7ad9\u70b9\u9996\u9009<\/td><\/tr><tr><td><code>WP_Query<\/code><\/td><td><em>O(n)<\/em><\/td><td>\u9ad8<\/td><td>\u5c0f\u578b\u7ad9\u70b9\u6216\u590d\u6742\u8fc7\u6ee4<\/td><\/tr><tr><td>\u76f4\u63a5SQL<\/td><td><em>O(1)<\/em><\/td><td>\u4f4e<\/td><td>\u9700\u624b\u52a8\u7ef4\u62a4\uff0c\u9ad8\u98ce\u9669<\/td><\/tr><\/tbody><\/table><\/figure><p>\u63a8\u8350\u59cb\u7ec8\u4f18\u5148\u4f7f\u7528 <strong><code>wp_count_posts()<\/code><\/strong>\uff0c\u5e76\u5728\u9700\u8981\u65f6\u6dfb\u52a0\u7f13\u5b58\u673a\u5236\u3002<\/p><h2 class=\"wp-block-heading\">\u767e\u4e07\u7ea7\u7ad9\u70b9\u7684\u9009\u62e9<\/h2><p>\u5728\u767e\u4e07\u7ea7WordPress\u7ad9\u70b9\u4e2d\uff0c<strong>\u4f18\u5148\u4f7f\u7528<code>wp_count_posts()<\/code><\/strong> \u662f\u66f4\u4f18\u9009\u62e9\uff0c\u4ec5\u5728\u6781\u5c11\u6570\u7279\u6b8a\u573a\u666f\u4e0b\u8003\u8651\u76f4\u63a5SQL\u67e5\u8be2\u3002\u4ee5\u4e0b\u662f\u6df1\u5ea6\u6027\u80fd\u5206\u6790\u4e0e\u5b9e\u8df5\u5efa\u8bae\uff1a<\/p><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\">\ud83d\udd25 \u767e\u4e07\u7ea7\u7ad9\u70b9\u6027\u80fd\u5173\u952e\u70b9<\/h3><ol class=\"wp-block-list\">\n<li><strong>\u7f13\u5b58\u673a\u5236\u51b3\u5b9a\u6027\u80fd<\/strong><br><code>wp_count_posts()<\/code> \u901a\u8fc7WordPress\u5bf9\u8c61\u7f13\u5b58\uff08\u5982Redis\/Memcached\uff09\u5b58\u50a8\u7ed3\u679c\uff0c<strong>\u9996\u6b21\u67e5\u8be2\u540e\u76f4\u63a5\u4ece\u5185\u5b58\u8bfb\u53d6<\/strong>\uff08\u5fae\u79d2\u7ea7\u54cd\u5e94\uff09\u3002\u800c\u76f4\u63a5SQL\u6bcf\u6b21\u9700\u626b\u63cf\u767e\u4e07\u884c\u6570\u636e\uff0c\u5373\u4f7f\u6709\u7d22\u5f15\uff0c\u5355\u6b21\u67e5\u8be2\u4ecd\u9700 <strong>50-200ms<\/strong>\uff08\u5b9e\u6d4bInnoDB\u8868\uff09\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u5e76\u53d1\u4e0b\u7684\u707e\u96be\u6027\u5dee\u5f02<\/strong><br>\u5f53QPS=100\u65f6\uff1a<\/li>\n<\/ol><ul class=\"wp-block-list\">\n<li><code>wp_count_posts()<\/code>\uff1a100%\u547d\u4e2d\u7f13\u5b58 \u2192 <strong>\u603b\u8017\u65f6 &lt; 0.1\u79d2<\/strong><\/li>\n\n\n\n<li>\u76f4\u63a5SQL\uff1a100\u6b21\u5168\u8868\u626b\u63cf \u2192 <strong>\u603b\u8017\u65f6 &gt; 5\u79d2<\/strong>\uff08\u5bfc\u81f4\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u8017\u5c3d\uff0c\u7ad9\u70b9\u5d29\u6e83\uff09<\/li>\n<\/ul><ol class=\"wp-block-list\">\n<li><strong>\u7f13\u5b58\u66f4\u65b0\u7b56\u7565<\/strong><br>WordPress\u5728\u6587\u7ae0\u589e\/\u5220\/\u6539\u65f6<strong>\u81ea\u52a8\u5237\u65b0<code>wp_count_posts<\/code>\u7f13\u5b58<\/strong>\uff0c\u786e\u4fdd\u6570\u636e\u5b9e\u65f6\u6027\u3002\u624b\u52a8\u5b9e\u73b0\u540c\u7b49\u903b\u8f91\u9700\u76d1\u542c\u591a\u4e2aHook\uff08\u5982<code>wp_insert_post<\/code>\uff09\uff0c\u4ee3\u7801\u590d\u6742\u5ea6\u5267\u589e\u3002<\/li>\n<\/ol><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\">\u26a0\ufe0f \u76f4\u63a5SQL\u7684\u98ce\u9669\u573a\u666f\uff08\u9700\u4e25\u683c\u89c4\u907f\uff09<\/h3><pre class=\"wp-block-code\"><code>\/\/ \u5371\u9669\u793a\u4f8b\uff1a\u672a\u7f13\u5b58+\u672a\u9632\u6ce8\u5165\n$total = $wpdb-&gt;get_var(\"SELECT COUNT(*) FROM wp_posts\"); <\/code><\/pre><ol class=\"wp-block-list\">\n<li><strong>\u5168\u8868\u626b\u63cf\u9501\u8868<\/strong>\uff1a<br>MyISAM\u8868\u6267\u884cCOUNT(*)\u4f1a\u9501\u8868\uff0c\u5bfc\u81f4\u5199\u5165\u963b\u585e\uff08InnoDB\u867d\u4e0d\u9501\u8868\uff0c\u4f46\u767e\u4e07\u884c\u626b\u63cf\u4ecd\u6d88\u8017IOPS\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7f13\u5b58\u7a7f\u900f<\/strong>\uff1a<br>\u7a81\u53d1\u6d41\u91cf\u65f6\u672a\u547d\u4e2d\u7f13\u5b58\uff0c\u76f4\u63a5\u51fb\u7a7f\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u4ece\u5ef6\u8fdf<\/strong>\uff1a<br>\u5728\u8bfb\u5199\u5206\u79bb\u67b6\u6784\u4e2d\uff0c\u7edf\u8ba1\u7ed3\u679c\u53ef\u80fd\u6ede\u540e\u4e8e\u4e3b\u5e93\u3002<\/li>\n<\/ol><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\">\u2705 \u5f3a\u5236\u4f18\u5316\u65b9\u6848\uff08\u82e5\u5fc5\u987b\u7ed5\u8fc7\u7f13\u5b58\uff09<\/h3><p>\u7ed3\u5408<strong>\u5bf9\u8c61\u7f13\u5b58+\u77ed\u5468\u671f\u66f4\u65b0<\/strong>\uff0c\u9002\u7528\u4e8e\u9700\u5b9e\u65f6\u7edf\u8ba1\u7684\u573a\u666f\uff1a<\/p><pre class=\"wp-block-code\"><code>function zzw_get_realtime_post_count($post_type = 'post', $status = 'publish') {\n    $cache_key = \"realtime_count_{$post_type}_{$status}\";\n    $count = wp_cache_get($cache_key, 'counts');\n\n    if (false === $count) {\n        global $wpdb;\n        $query = $wpdb-&gt;prepare(\n            \"SELECT COUNT(*) FROM {$wpdb-&gt;posts} \n             WHERE post_type = %s AND post_status = %s\",\n            $post_type, $status\n        );\n        $count = (int)$wpdb-&gt;get_var($query);\n        \/\/ \u8bbe\u7f6e5\u79d2\u77ed\u7f13\u5b58\uff0c\u5e73\u8861\u5b9e\u65f6\u6027\u4e0e\u6027\u80fd\n        wp_cache_set($cache_key, $count, 'counts', 5); \n    }\n    return $count;\n}<\/code><\/pre><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\">\ud83d\udcca \u51b3\u7b56\u6d41\u7a0b\u56fe<\/h3><pre class=\"wp-block-code\"><code>graph TD\n    A[\u9700\u8981\u6587\u7ae0\u7edf\u8ba1] --&gt; B{\u662f\u5426\u8981\u6c42\u7edd\u5bf9\u5b9e\u65f6\uff1f}\n    B --&gt;|\u662f| C[\u4f7f\u7528\u5e265\u79d2\u7f13\u5b58\u7684SQL\u65b9\u6848]\n    B --&gt;|\u5426| D[\u4f7f\u7528wp_count_posts]\n    C --&gt; E{\u6d41\u91cf\u662f\u5426&gt;100QPS\uff1f}\n    E --&gt;|\u662f| F[\u589e\u52a0Redis\u7f13\u5b58+\u5f02\u6b65\u66f4\u65b0]\n    E --&gt;|\u5426| G[\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c]\n    D --&gt; H[\u4eab\u53d7\u5185\u7f6e\u7f13\u5b58\u4f18\u52bf]<\/code><\/pre><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\">\ud83d\udca1 \u7ec8\u6781\u5efa\u8bae<\/h3><ol class=\"wp-block-list\">\n<li><strong>\u5e38\u89c4\u573a\u666f<\/strong>\uff1a<br>\u575a\u6301\u4f7f\u7528<code>wp_count_posts()<\/code>\uff0c\u901a\u8fc7 <strong>Redis\/Memcached \u52a0\u901f\u5bf9\u8c61\u7f13\u5b58<\/strong>\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u65f6\u6027\u8981\u6c42\u9ad8<\/strong>\uff1a<br>\u7528<code>zzw_get_realtime_post_count()<\/code>\u65b9\u6848\uff0c<strong>\u7f13\u5b58\u5468\u671f\u8bbe\u4e3a5-30\u79d2<\/strong>\u3002<\/li>\n\n\n\n<li><strong>\u5343\u4e07\u7ea7\u6570\u636e\u4f18\u5316<\/strong>\uff1a<br>\u5728<code>wp_postmeta<\/code>\u4e2d\u5b58\u50a8\u8ba1\u6570\u5668\uff0c\u66f4\u65b0\u6587\u7ae0\u65f6\u539f\u5b50\u64cd\u4f5c\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   update_post_meta($post_id, '_post_count', $count+1); \/\/ \u5199\u64cd\u4f5c\u4ec5+1<\/code><\/pre><p><strong>\u8bfb\u64cd\u4f5c\u76f4\u63a5\u8bfbmeta\u503c<\/strong>\uff080ms\u54cd\u5e94\uff09\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u5728WordPress\u5927\u578b\u7f51\u7ad9\u4e2d\uff0c\u83b7\u53d6post\u548cchan\u4e24\u79cd\u6587\u7ae0\u7c7b\u578b\u7684\u5168\u90e8\u6587\u7ae0\u6570\u91cf\uff08\u9ed8\u8ba4\u7edf\u8ba1\u5df2\u53d1\u5e03\u72b6\u6001\uff09\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u9ad8\u6548\u65b9\u6cd5\u5b9e\u73b0\uff1a \u63a8\u8350\u65b9\u6cd5\uff1awp_count_posts()\u51fd\u6570 \u539f\u7406\uff1a\u76f4\u63a5\u8c03\u7528WordPress\u6838\u5fc3\u51fd\u6570\uff0c\u4ece\u6570\u636e\u5e93\u7f13\u5b58\u4e2d\u8bfb\u53d6\u7edf\u8ba1\u503c\uff0c\u6027\u80fd\u6700\u4f18\uff08\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\uff09\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a \u4f18\u52bf\uff1a \u65e0\u9700\u52a0\u8f7d\u6587\u7ae0\u5bf9\u8c61\uff0c..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":true},"tags":[],"bjjc2nav":[14],"tuisongtax":[],"class_list":["post-4210","bjjc","type-bjjc","status-publish","hentry","bjjc2nav-jdztkf"],"acf":{"qian_art_seotitle":"WordPress\u83b7\u53d6\u6587\u7ae0\u603b\u6570\u91cf\u7684\u65b9\u6cd5\uff0c\u767e\u4e07\u5343\u4e07\u7ea7\u6570\u636e\u4f18\u5316\u65b9\u6848","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":"WordPress\u83b7\u53d6\u6587\u7ae0\u603b\u6570\u91cf\u7684\u65b9\u6cd5\uff0c\u767e\u4e07\u5343\u4e07\u7ea7\u6570\u636e\u4f18\u5316\u65b9\u6848"},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"\u83b7\u53d6\u6587\u7ae0\u7c7b\u578b\u4e2d\u6587\u7ae0\u7684\u603b\u6570\u91cf","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":"\u83b7\u53d6\u6587\u7ae0\u7c7b\u578b\u4e2d\u6587\u7ae0\u7684\u603b\u6570\u91cf"}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc\/4210","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=4210"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tags?post=4210"},{"taxonomy":"bjjc2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc2nav?post=4210"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tuisongtax?post=4210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}