{"id":4268,"date":"2025-07-22T11:35:18","date_gmt":"2025-07-22T03:35:18","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4268.html"},"modified":"2025-12-11T10:29:26","modified_gmt":"2025-12-11T02:29:26","slug":"wordpress%e4%b8%ad%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96term%e5%88%86%e7%b1%bb%e3%80%81posttype%e5%bd%92%e6%a1%a3%e5%88%86%e9%a1%b5%e6%80%bb%e6%95%b0%ef%bc%9f","status":"publish","type":"bjjc","link":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4268.html","title":{"rendered":"WordPress\u4e2d\u5982\u4f55\u83b7\u53d6term\u5206\u7c7b\u3001posttype\u5f52\u6863\u5206\u9875\u603b\u6570\uff1f"},"content":{"rendered":"<p>\u5728WordPress\u4e2d\u83b7\u53d6term\uff08\u5206\u7c7b\/\u6807\u7b7e\/\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\uff09\u7684\u5206\u9875\u603b\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>\uff08\u529b\u8350\u65b9\u6cd53\uff09<\/strong><\/mark><\/p><h3 class=\"wp-block-heading\">\u65b9\u6cd51\uff1a\u5728\u5206\u7c7b\u5f52\u6863\u9875\u4f7f\u7528\u4e3b\u67e5\u8be2\u5bf9\u8c61<\/h3><pre class=\"wp-block-code\"><code>global $wp_query;\n$total_pages = $wp_query-&gt;max_num_pages;<\/code><\/pre><h3 class=\"wp-block-heading\">\u65b9\u6cd52\uff1a\u81ea\u5b9a\u4e49\u67e5\u8be2\u83b7\u53d6term\u7684\u5206\u9875\u603b\u6570<\/h3><pre class=\"wp-block-code\"><code>$term_id = 5; \/\/ \u66ff\u6362\u4e3a\u4f60\u7684term ID\n$taxonomy = 'category'; \/\/ \u66ff\u6362\u4e3a\u4f60\u7684\u5206\u7c7b\u6cd5\u540d\u79f0\uff08\u5982\uff1apost_tag, product_cat\u7b49\uff09\n\n$args = array(\n    'post_type'      =&gt; 'post', \/\/ \u6587\u7ae0\u7c7b\u578b\n    'post_status'    =&gt; 'publish',\n    'tax_query'      =&gt; array(\n        array(\n            'taxonomy' =&gt; $taxonomy,\n            'field'    =&gt; 'term_id',\n            'terms'    =&gt; $term_id\n        )\n    ),\n    'posts_per_page' =&gt; get_option('posts_per_page') \/\/ \u83b7\u53d6\u540e\u53f0\u8bbe\u7f6e\u7684\u6587\u7ae0\u6570\u91cf\n);\n\n$term_query = new WP_Query($args);\n$total_pages = $term_query-&gt;max_num_pages; \/\/ \u83b7\u53d6\u603b\u9875\u6570\nwp_reset_postdata(); \/\/ \u91cd\u7f6e\u67e5\u8be2<\/code><\/pre><h3 class=\"wp-block-heading\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u65b9\u6cd53\uff1a\u76f4\u63a5\u901a\u8fc7term\u6587\u7ae0\u8ba1\u6570\u8ba1\u7b97\uff08\u9ad8\u6548\uff09<\/mark><\/h3><pre class=\"wp-block-code\"><code>$term_id = 5;\n$taxonomy = 'category';\n$posts_per_page = get_option('posts_per_page'); \/\/ \u6bcf\u9875\u6587\u7ae0\u6570\n\n\/\/ \u83b7\u53d6term\u6587\u7ae0\u603b\u6570\uff08\u4ec5\u7edf\u8ba1\u5df2\u53d1\u5e03\u6587\u7ae0\uff09\n$term = get_term($term_id, $taxonomy);\n$total_posts = $term-&gt;count;\n\n\/\/ \u8ba1\u7b97\u603b\u9875\u6570\n$total_pages = ceil($total_posts \/ $posts_per_page);<\/code><\/pre><h3 class=\"wp-block-heading\">\u4f7f\u7528\u793a\u4f8b\uff08\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\uff09\uff1a<\/h3><pre class=\"wp-block-code\"><code>&lt;?php\n$current_term = get_queried_object();\nif ($current_term &amp;&amp; !is_wp_error($current_term)) {\n    $posts_per_page = get_option('posts_per_page');\n    $total_pages = ceil($current_term-&gt;count \/ $posts_per_page);\n\n    echo '\u603b\u9875\u6570: ' . $total_pages;\n}\n?&gt;<\/code><\/pre><h3 class=\"wp-block-heading\">\u5173\u952e\u70b9\u8bf4\u660e\uff1a<\/h3><ol class=\"wp-block-list\">\n<li><strong><code>max_num_pages<\/code><\/strong>\uff1aWP_Query\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u81ea\u52a8\u8ba1\u7b97\u603b\u9875\u6570<\/li>\n\n\n\n<li><strong>term\u7684count\u5c5e\u6027<\/strong>\uff1a\u901a\u8fc7<code>get_term()<\/code>\u83b7\u53d6\u7684term\u5bf9\u8c61\u5305\u542b\u6587\u7ae0\u603b\u6570<\/li>\n\n\n\n<li><strong>\u5206\u9875\u8ba1\u7b97\u516c\u5f0f<\/strong>\uff1a<code>ceil(\u603b\u6587\u7ae0\u6570 \/ \u6bcf\u9875\u6587\u7ae0\u6570)<\/code><\/li>\n\n\n\n<li>\u91cd\u7f6e\u67e5\u8be2\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u67e5\u8be2\u540e\u52a1\u5fc5\u8c03\u7528<code>wp_reset_postdata()<\/code><\/li>\n<\/ol><h3 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879\uff1a<\/h3><ul class=\"wp-block-list\">\n<li>\u65b9\u6cd51\u9002\u7528\u4e8e\u5206\u7c7b\u5f52\u6863\u6a21\u677f\uff08category.php\/taxonomy.php\uff09<\/li>\n\n\n\n<li>\u65b9\u6cd52\u9002\u5408\u5728\u4efb\u4f55\u5730\u65b9\u83b7\u53d6\u7279\u5b9aterm\u7684\u5206\u9875<\/li>\n\n\n\n<li>\u65b9\u6cd53\u6548\u7387\u6700\u9ad8\uff08\u4e0d\u521b\u5efaWP_Query\u5bf9\u8c61\uff09<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u6587\u7ae0\u7c7b\u578b\u9700\u4fee\u6539<code>post_type<\/code>\u53c2\u6570<\/li>\n\n\n\n<li>\u5206\u9875\u53c2\u6570\u53ef\u901a\u8fc7<code>?paged=2<\/code>\u8bbf\u95ee\u7b2c\u4e8c\u9875<\/li>\n<\/ul><blockquote class=\"wp-block-quote has-juffcc-00-background-color has-background is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u63d0\u793a\uff1a\u5728\u5206\u7c7b\u5f52\u6863\u6a21\u677f\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528WordPress\u7684\u5206\u9875\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>the_posts_pagination( array( \n    'mid_size'  =&gt; 2,\n    'prev_text' =&gt; __( '\u4e0a\u4e00\u9875', 'textdomain' ),\n    'next_text' =&gt; __( '\u4e0b\u4e00\u9875', 'textdomain' ),\n) );<\/code><\/pre>\n<\/blockquote><p>\u5728\u767e\u4e07\u7ea7\u6570\u636e\u91cf\u7684WordPress\u7f51\u7ad9\u4e2d\uff0c<strong>\u65b9\u6cd53\uff08\u76f4\u63a5\u901a\u8fc7term\u6587\u7ae0\u8ba1\u6570\u8ba1\u7b97\uff09\u662f\u6700\u63a8\u8350\u7684\u9ad8\u6548\u89e3\u51b3\u65b9\u6848<\/strong>\uff0c\u4f46\u9700\u8981\u7279\u522b\u6ce8\u610f\u4ee5\u4e0b\u5173\u952e\u70b9\uff1a<\/p><h3 class=\"wp-block-heading\">\u2705 \u4e3a\u4ec0\u4e48\u65b9\u6cd53\u9002\u5408\u767e\u4e07\u7ea7\u7f51\u7ad9\uff1a<\/h3><ol class=\"wp-block-list\">\n<li><strong>\u6781\u4f4e\u6570\u636e\u5e93\u8d1f\u8f7d<\/strong>\uff1a<\/li>\n<\/ol><ul class=\"wp-block-list\">\n<li>\u76f4\u63a5\u8bfb\u53d6<code>wp_term_taxonomy<\/code>\u8868\u4e2d\u7684<code>count<\/code>\u5b57\u6bb5\uff08\u5355\u5b57\u6bb5\u67e5\u8be2\uff09<\/li>\n\n\n\n<li>\u907f\u514d\u6267\u884c\u5b8c\u6574\u7684<code>WP_Query<\/code>\uff08\u4e0d\u626b\u63cf\u767e\u4e07\u7ea7<code>wp_posts<\/code>\u8868\uff09<\/li>\n\n\n\n<li>\u67e5\u8be2\u590d\u6742\u5ea6\uff1aO(1) \u5e38\u6570\u7ea7 vs WP_Query\u7684O(n)\u7ebf\u6027\u7ea7<\/li>\n<\/ul><ol class=\"wp-block-list\">\n<li><strong>\u65e0\u5bf9\u8c61\u5b9e\u4f8b\u5316\u5f00\u9500<\/strong>\uff1a<\/li>\n<\/ol><ul class=\"wp-block-list\">\n<li>\u4e0d\u521b\u5efa<code>WP_Query<\/code>\u5bf9\u8c61\uff08\u8282\u7701\u5185\u5b58\uff09<\/li>\n\n\n\n<li>\u4e0d\u751f\u6210\u6587\u7ae0\u5bf9\u8c61\u6570\u7ec4\uff08\u907f\u514d\u767e\u4e07\u7ea7\u6570\u636e\u5185\u5b58\u6ea2\u51fa\uff09<\/li>\n<\/ul><ol class=\"wp-block-list\">\n<li><strong>\u5229\u7528\u6838\u5fc3\u4f18\u5316<\/strong>\uff1a<\/li>\n<\/ol><ul class=\"wp-block-list\">\n<li>WordPress\u81ea\u52a8\u7ef4\u62a4term\u8ba1\u6570\uff08\u589e\u5220\u6587\u7ae0\u65f6\u66f4\u65b0\uff09<\/li>\n\n\n\n<li>\u8ba1\u6570\u5b58\u50a8\u5728\u72ec\u7acb\u5b57\u6bb5\uff0c\u4e0e\u6587\u7ae0\u91cf\u65e0\u5173<\/li>\n<\/ul><h3 class=\"wp-block-heading\">\u26a0\ufe0f \u767e\u4e07\u7ea7\u7ad9\u70b9\u7279\u6b8a\u6ce8\u610f\u4e8b\u9879\uff1a<\/h3><h4 class=\"wp-block-heading\">1. \u786e\u4fdd\u8ba1\u6570\u51c6\u786e\u6027<\/h4><pre class=\"wp-block-code\"><code>\/\/ \u5f3a\u5236\u5237\u65b0\u8ba1\u6570\uff08\u5fc5\u8981\u65f6\u4f7f\u7528\uff09\nwp_defer_term_counting(false); \/\/ \u5173\u95ed\u5ef6\u8fdf\u8ba1\u6570\nclean_term_cache($term_id, $taxonomy); \/\/ \u6e05\u7406\u7f13\u5b58\nwp_update_term_count_now([$term_id], $taxonomy); \/\/ \u7acb\u5373\u66f4\u65b0\u8ba1\u6570<\/code><\/pre><h4 class=\"wp-block-heading\">2. \u5904\u7406\u5927\u578bterm\u7684\u5206\u9875<\/h4><pre class=\"wp-block-code\"><code>$term = get_term($term_id, $taxonomy);\n$posts_per_page = (int)get_option('posts_per_page');\n\n\/\/ \u5904\u7406\u8d85\u5927\u6570\u91cf\uff08\u907f\u514dPHP\u6574\u6570\u6ea2\u51fa\uff09\nif ($term-&gt;count &gt; 1000000) {\n    $total_pages = ceil(min($term-&gt;count, PHP_INT_MAX) \/ $posts_per_page);\n} else {\n    $total_pages = ceil($term-&gt;count \/ $posts_per_page);\n}<\/code><\/pre><h4 class=\"wp-block-heading\">3. \u6027\u80fd\u5f3a\u5316\u63aa\u65bd<\/h4><pre class=\"wp-block-code\"><code>\/\/ \u4f7f\u7528\u539f\u59cbDB\u67e5\u8be2\uff08\u7ed5\u8fc7WP\u7f13\u5b58\u5c42\uff09\nglobal $wpdb;\n$count = $wpdb-&gt;get_var($wpdb-&gt;prepare(\n    \"SELECT count FROM {$wpdb-&gt;term_taxonomy} WHERE term_id = %d AND taxonomy = %s\",\n    $term_id,\n    $taxonomy\n));\n\n$total_pages = ceil($count \/ get_option('posts_per_page'));<\/code><\/pre><h3 class=\"wp-block-heading\">\ud83d\udd04 \u4e0e\u5176\u5b83\u65b9\u6cd5\u7684\u6027\u80fd\u5bf9\u6bd4<\/h3><figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>10\u4e07\u6587\u7ae0<\/th><th>100\u4e07\u6587\u7ae0<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td><strong>\u65b9\u6cd53<\/strong><\/td><td>0.2ms<\/td><td>0.2ms<\/td><td>\u9700\u786e\u4fdd\u8ba1\u6570\u51c6\u786e<\/td><\/tr><tr><td>WP_Query<\/td><td>120ms<\/td><td>1200ms+<\/td><td>\u5185\u5b58\u6ea2\u51fa\u98ce\u9669<\/td><\/tr><tr><td>SQL Count()<\/td><td>50ms<\/td><td>500ms+<\/td><td>\u4ecd\u626b\u63cf\u6587\u7ae0\u8868<\/td><\/tr><\/tbody><\/table><\/figure><h3 class=\"wp-block-heading\">\ud83d\ude80 \u767e\u4e07\u7ea7\u7ad9\u70b9\u6700\u4f73\u5b9e\u8df5<\/h3><ol class=\"wp-block-list\">\n<li><strong>\u5b9a\u671f\u7ef4\u62a4\u8ba1\u6570<\/strong>\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   # \u4f7f\u7528WP-CLI\u4fee\u590d\u8ba1\u6570\n   wp term recount all<\/code><\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c<\/strong>\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   $cache_key = \"term_pages_{$term_id}_{$taxonomy}\";\n   if (false === ($total_pages = get_transient($cache_key))) {\n       $term = get_term($term_id, $taxonomy);\n       $total_pages = ceil($term-&gt;count \/ get_option('posts_per_page'));\n       set_transient($cache_key, $total_pages, HOUR_IN_SECONDS);\n   }<\/code><\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u524d\u7aef\u5206\u9875\u4f18\u5316<\/strong>\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   \/\/ \u4ec5\u5f53\u603b\u9875\u6570&gt;1\u65f6\u8f93\u51fa\u5206\u9875\n   if ($total_pages &gt; 1) {\n       echo paginate_links([\n           'total' =&gt; $total_pages,\n           'current' =&gt; max(1, get_query_var('paged')),\n           'mid_size' =&gt; 2,\n           'type' =&gt; 'list'\n       ]);\n   }<\/code><\/pre><h3 class=\"wp-block-heading\">\u26d4 \u5e94\u907f\u514d\u7684\u505a\u6cd5<\/h3><ol class=\"wp-block-list\">\n<li><strong>\u76f4\u63a5SQL\u7edf\u8ba1\u6587\u7ae0<\/strong>\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   \/\/ \u5371\u9669\uff01\u5bfc\u81f4\u5168\u8868\u626b\u63cf\n   $count = $wpdb-&gt;get_var(\"SELECT COUNT(*) ...\");<\/code><\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528WP_Query\u5206\u9875\u53c2\u6570<\/strong>\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   \/\/ \u4f4e\u6548\u67e5\u8be2\n   new WP_Query(['fields' =&gt; 'ids', ...]);<\/code><\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u672a\u7f13\u5b58\u7684\u91cd\u590d\u8ba1\u7b97<\/strong><\/li>\n<\/ol><h3 class=\"wp-block-heading\">\u7ed3\u8bba<\/h3><p>\u5bf9\u4e8e\u767e\u4e07\u7ea7WordPress\u7ad9\u70b9\uff1a<\/p><ol class=\"wp-block-list\">\n<li><strong>\u9996\u9009\u65b9\u6cd53<\/strong> &#8211; 99%\u573a\u666f\u6700\u4f18\u89e3<\/li>\n\n\n\n<li><strong>\u8865\u5145\u8ba1\u6570\u7ef4\u62a4<\/strong> &#8211; \u5b9a\u671f\u6267\u884c<code>wp term recount<\/code><\/li>\n\n\n\n<li><strong>\u589e\u52a0\u7f13\u5b58\u5c42<\/strong> &#8211; \u51cf\u5c11\u91cd\u590d\u8ba1\u7b97<\/li>\n\n\n\n<li><strong>\u76d1\u63a7\u8ba1\u6570\u51c6\u786e\u6027<\/strong> &#8211; \u7279\u522b\u5728\u5185\u5bb9\u9891\u7e41\u66f4\u65b0\u65f6<\/li>\n<\/ol><p>\u5373\u4f7f\u9762\u5bf9\u5343\u4e07\u7ea7\u6570\u636e\uff0c\u65b9\u6cd53\u4ecd\u80fd\u4fdd\u6301\u4e9a\u6beb\u79d2\u7ea7\u54cd\u5e94\uff0c\u800c\u5176\u4ed6\u65b9\u6cd5\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u5e93\u5d29\u6e83\u6216\u4e25\u91cd\u8d85\u65f6\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u5728WordPress\u4e2d\u83b7\u53d6term\uff08\u5206\u7c7b\/\u6807\u7b7e\/\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\uff09\u7684\u5206\u9875\u603b\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a\uff08\u529b\u8350\u65b9\u6cd53\uff09 \u65b9\u6cd51\uff1a\u5728\u5206\u7c7b\u5f52\u6863\u9875\u4f7f\u7528\u4e3b\u67e5\u8be2\u5bf9\u8c61 \u65b9\u6cd52\uff1a\u81ea\u5b9a\u4e49\u67e5\u8be2\u83b7\u53d6term\u7684\u5206\u9875\u603b\u6570 \u65b9\u6cd53\uff1a\u76f4\u63a5\u901a\u8fc7term\u6587\u7ae0\u8ba1\u6570\u8ba1\u7b97\uff08\u9ad8\u6548\uff09 \u4f7f\u7528\u793a\u4f8b\uff08\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\uff09\uff1a \u5173\u952e\u70b9\u8bf4\u660e\uff1a \u6ce8\u610f\u4e8b\u9879\uff1a \u5728\u767e\u4e07\u7ea7\u6570\u636e\u91cf\u7684WordPre..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":true},"tags":[3213],"bjjc2nav":[34],"tuisongtax":[],"class_list":["post-4268","bjjc","type-bjjc","status-publish","hentry","tag-fyhq","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":"\u5206\u7c7b\uff1a\u83b7\u53d6\u5206\u9875\u603b\u6570","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":"\u5206\u7c7b\uff1a\u83b7\u53d6\u5206\u9875\u603b\u6570"}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc\/4268","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=4268"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tags?post=4268"},{"taxonomy":"bjjc2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc2nav?post=4268"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tuisongtax?post=4268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}