{"id":4726,"date":"2025-10-20T11:04:59","date_gmt":"2025-10-20T03:04:59","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4726.html"},"modified":"2025-10-20T11:05:00","modified_gmt":"2025-10-20T03:05:00","slug":"wordpress%e5%88%86%e7%b1%bb%e6%96%87%e7%ab%a0%e6%80%bb%e6%95%b0%e8%ae%a1%e7%ae%97%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%96%b9%e6%b3%95%e5%8f%8a%e4%bb%a3%e7%a0%81","status":"publish","type":"bjjc","link":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4726.html","title":{"rendered":"WordPress\u5206\u7c7b\u6587\u7ae0\u603b\u6570\u8ba1\u7b97\u7684\u5b9e\u73b0\u65b9\u6cd5\u53ca\u4ee3\u7801"},"content":{"rendered":"<p>WordPress\u5206\u7c7b\u6587\u7ae0\u603b\u6570\u8ba1\u7b97\u5e76\u975e\u90a3\u4e48\u7b80\u6d01\uff0c\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a<\/p><p>1\u3001\u5982\u679c\u8be5\u5206\u7c7b\u5b58\u5728\u5b50\u5206\u7c7b\u6216\u591a\u5c42\u7ea7\u5b50\u5206\u7c7b\uff0c\u5e94\u5c06\u8be5\u5206\u7c7b\u53ca\u5176\u4e0b\u6240\u6709\u5c42\u7ea7\u5b50\u5206\u7c7b\u4e2d\u7684\u6587\u7ae0\u5168\u90e8\u8ba1\u7b97\u5728\u5185\u3002 <\/p><p>2\u3001\u5982\u679c\u8be5\u5206\u7c7b\u6ca1\u6709\u5b50\u5206\u7c7b\uff0c\u5219\u8ba1\u7b97\u8be5\u5206\u7c7b\u4e2d\u7684\u6587\u7ae0\u603b\u6570\u3002<\/p><h2 class=\"wp-block-heading\"><strong>SQL \u7248\u672c<\/strong><\/h2><pre class=\"wp-block-code\"><code>public function get_tax_posts_count_with_children($term, $taxonomy) {\n    \/\/ \u786e\u4fdd $term \u662f\u6709\u6548\u7684\u672f\u8bed\u5bf9\u8c61\n    if (is_numeric($term)) {\n        $term = get_term($term, $taxonomy);\n    }\n\n    if (is_wp_error($term) || !$term) {\n        return 0;\n    }\n\n    \/\/ \u83b7\u53d6\u6240\u6709\u5b50\u5206\u7c7bID\uff08\u5305\u62ec\u5b50\u5b59\u5206\u7c7b\uff09\n    $child_terms = get_term_children($term-&gt;term_id, $taxonomy);\n    $term_ids = [];\n\n    \/\/ \u6dfb\u52a0\u5f53\u524d\u5206\u7c7bID\n    $term_ids[] = (int) $term-&gt;term_id;\n\n    \/\/ \u6dfb\u52a0\u6240\u6709\u6709\u6548\u7684\u5b50\u5206\u7c7bID\n    if (!is_wp_error($child_terms) &amp;&amp; !empty($child_terms)) {\n        foreach ($child_terms as $child_term_id) {\n            $term_ids[] = (int) $child_term_id;\n        }\n    }\n\n    \/\/ \u53bb\u91cd\n    $term_ids = array_unique($term_ids);\n\n    \/\/ \u5982\u679c\u53ea\u6709\u4e00\u4e2a\u5206\u7c7b\uff08\u6ca1\u6709\u5b50\u5206\u7c7b\uff09\uff0c\u76f4\u63a5\u67e5\u8be2\u8be5\u5206\u7c7b\u7684\u6587\u7ae0\u6570\u91cf\n    if (count($term_ids) === 1) {\n        return (int) $term-&gt;count;\n    }\n\n    \/\/ \u591a\u4e2a\u5206\u7c7b\u65f6\u8fdb\u884c\u8054\u8868\u67e5\u8be2\n    global $wpdb;\n\n    $placeholders = implode(',', array_fill(0, count($term_ids), '%d'));\n\n    \/\/ \u83b7\u53d6\u6240\u6709\u516c\u5f00\u7684\u6587\u7ae0\u7c7b\u578b\n    $post_types = get_post_types(array('public' =&gt; true));\n    $post_type_placeholders = implode(',', array_fill(0, count($post_types), '%s'));\n\n    \/\/ \u5408\u5e76\u6240\u6709\u53c2\u6570\n    $params = array_merge($term_ids, $post_types, [$taxonomy]);\n\n    $query = $wpdb-&gt;prepare(\n        \"SELECT COUNT(DISTINCT p.ID)\n        FROM {$wpdb-&gt;posts} p\n        INNER JOIN {$wpdb-&gt;term_relationships} tr ON p.ID = tr.object_id\n        INNER JOIN {$wpdb-&gt;term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id\n        WHERE tt.term_id IN ($placeholders)\n        AND p.post_status = 'publish'\n        AND p.post_type IN ($post_type_placeholders)\n        AND tt.taxonomy = %s\",\n        $params\n    );\n\n    $count = (int) $wpdb-&gt;get_var($query);\n\n    return $count;\n}<\/code><\/pre><p><strong>\u4e3b\u8981\u5b9e\u73b0\uff1a<\/strong><\/p><ol class=\"wp-block-list\">\n<li><strong>\u83b7\u53d6\u6240\u6709\u516c\u5f00\u7684\u6587\u7ae0\u7c7b\u578b<\/strong>\uff1a\u4f7f\u7528 <code>get_post_types(array('public' =&gt; true))<\/code> \u83b7\u53d6\u6240\u6709\u516c\u5f00\u7684\u6587\u7ae0\u7c7b\u578b<\/li>\n\n\n\n<li><strong>\u52a8\u6001\u6784\u5efa\u6587\u7ae0\u7c7b\u578b\u6761\u4ef6<\/strong>\uff1a\u4f7f\u7528 <code>IN<\/code> \u6761\u4ef6\u6765\u5305\u542b\u6240\u6709\u516c\u5f00\u7684\u6587\u7ae0\u7c7b\u578b<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u5904\u7406<\/strong>\uff1a\u6b63\u786e\u5408\u5e76\u672f\u8bedID\u3001\u6587\u7ae0\u7c7b\u578b\u548c\u5206\u7c7b\u6cd5\u53c2\u6570<\/li>\n<\/ol><h2 class=\"wp-block-heading\"><strong><strong>WP_Query \u7248\u672c<\/strong><\/strong><\/h2><p>\u5982\u679c\u60a8\u66f4\u559c\u6b22\u4f7f\u7528 WordPress \u539f\u751f\u51fd\u6570\uff0c\u8fd9\u91cc\u662f\u4e00\u4e2a\u4f7f\u7528 WP_Query \u7684\u7248\u672c\uff1a<\/p><pre class=\"wp-block-code\"><code>public function get_tax_posts_count_with_children($term, $taxonomy) {\n    \/\/ \u786e\u4fdd $term \u662f\u6709\u6548\u7684\u672f\u8bed\u5bf9\u8c61\n    if (is_numeric($term)) {\n        $term = get_term($term, $taxonomy);\n    }\n\n    if (is_wp_error($term) || !$term) {\n        return 0;\n    }\n\n    \/\/ \u83b7\u53d6\u6240\u6709\u5b50\u5206\u7c7bID\uff08\u5305\u62ec\u5f53\u524d\u5206\u7c7b\uff09\n    $child_terms = get_term_children($term-&gt;term_id, $taxonomy);\n    $term_ids = [(int) $term-&gt;term_id];\n\n    if (!is_wp_error($child_terms) &amp;&amp; !empty($child_terms)) {\n        $term_ids = array_merge($term_ids, array_map('intval', $child_terms));\n        $term_ids = array_unique($term_ids);\n\n        \/\/ \u4f7f\u7528 WP_Query \u7edf\u8ba1\u6240\u6709\u516c\u5f00\u6587\u7ae0\u7c7b\u578b\u7684\u6587\u7ae0\u6570\u91cf\n        $query_args = [\n            'post_type' =&gt; get_post_types(array('public' =&gt; true)), \/\/ \u6240\u6709\u516c\u5f00\u6587\u7ae0\u7c7b\u578b\n            'post_status' =&gt; 'publish',\n            'fields' =&gt; 'ids',\n            'posts_per_page' =&gt; -1,\n            'tax_query' =&gt; [\n                [\n                    'taxonomy' =&gt; $taxonomy,\n                    'field' =&gt; 'term_id',\n                    'terms' =&gt; $term_ids,\n                    'operator' =&gt; 'IN',\n                ]\n            ]\n        ];\n\n        $query = new WP_Query($query_args);\n        return (int) $query-&gt;found_posts;\n    }\n\n    \/\/ \u6ca1\u6709\u5b50\u5206\u7c7b\u65f6\u76f4\u63a5\u8fd4\u56de\u5f53\u524d\u5206\u7c7b\u8ba1\u6570\n    return (int) $term-&gt;count;\n}<\/code><\/pre><h2 class=\"wp-block-heading\"><strong>\u4e24\u79cd\u65b9\u6848\u7684\u6bd4\u8f83<\/strong><\/h2><ul class=\"wp-block-list\">\n<li><strong>SQL \u7248\u672c<\/strong>\uff1a\u6027\u80fd\u66f4\u597d\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u6570\u636e<\/li>\n\n\n\n<li><strong>WP_Query \u7248\u672c<\/strong>\uff1a\u66f4\u7b26\u5408 WordPress \u5f00\u53d1\u89c4\u8303\uff0c\u66f4\u5bb9\u6613\u7ef4\u62a4<\/li>\n<\/ul><p>\u4e24\u4e2a\u7248\u672c\u90fd\u80fd\u6b63\u786e\u7edf\u8ba1\u6240\u6709\u516c\u5f00\u6587\u7ae0\u7c7b\u578b\uff08post, page, \u4ee5\u53ca\u4efb\u4f55\u81ea\u5b9a\u4e49\u7684\u516c\u5f00\u6587\u7ae0\u7c7b\u578b\uff09\u5728\u6307\u5b9a\u5206\u7c7b\u53ca\u5176\u5b50\u5206\u7c7b\u4e2d\u7684\u6587\u7ae0\u603b\u6570\u3002<\/p><p>\u63a8\u8350\u4f7f\u7528 SQL \u7248\u672c\uff0c\u56e0\u4e3a\u5b83\u5728\u5904\u7406\u5927\u91cf\u5206\u7c7b\u548c\u6587\u7ae0\u65f6\u6027\u80fd\u66f4\u4f18\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>WordPress\u5206\u7c7b\u6587\u7ae0\u603b\u6570\u8ba1\u7b97\u5e76\u975e\u90a3\u4e48\u7b80\u6d01\uff0c\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a 1\u3001\u5982\u679c\u8be5\u5206\u7c7b\u5b58\u5728\u5b50\u5206\u7c7b\u6216\u591a\u5c42\u7ea7\u5b50\u5206\u7c7b\uff0c\u5e94\u5c06\u8be5\u5206\u7c7b\u53ca\u5176\u4e0b\u6240\u6709\u5c42\u7ea7\u5b50\u5206\u7c7b\u4e2d\u7684\u6587\u7ae0\u5168\u90e8\u8ba1\u7b97\u5728\u5185\u3002 2\u3001\u5982\u679c\u8be5\u5206\u7c7b\u6ca1\u6709\u5b50\u5206\u7c7b\uff0c\u5219\u8ba1\u7b97\u8be5\u5206\u7c7b\u4e2d\u7684\u6587\u7ae0\u603b\u6570\u3002 SQL \u7248\u672c \u4e3b\u8981\u5b9e\u73b0\uff1a WP_Query \u7248\u672c \u5982\u679c\u60a8\u66f4\u559c\u6b22\u4f7f\u7528 WordPress \u539f..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":true},"tags":[2107,2106,59],"bjjc2nav":[34],"tuisongtax":[],"class_list":["post-4726","bjjc","type-bjjc","status-publish","hentry","tag-cjyy","tag-cjzn","tag-pzjc","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\u6587\u7ae0\u603b\u6570\u8ba1\u7b97","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":"\u5206\u7c7b\u6587\u7ae0\u603b\u6570\u8ba1\u7b97"}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc\/4726","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=4726"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tags?post=4726"},{"taxonomy":"bjjc2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc2nav?post=4726"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tuisongtax?post=4726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}