如何通过数据库 (SQL) 获取 Wordpress 类别 (the_categories())
How to get Wordpress categories (the_categories()) via DB (SQL)
我需要什么以及为什么
由于我的插件 (WC_Membership) get_categories()
由于内容受限而被过滤。
现在我需要列出每个类别以进行比较(我会通过 get_categories()
这样做),但如果不是管理员,则不会列出所有类别。 SQL 是解决我的问题的方法吗? (不要使用 WP_Query -> 会被其他插件过滤)
TL;DR:我需要未过滤的类别 - 可能来自 SQL
通常我会...
$cats = [];
foreach (get_categories() as $key => $value) {
$cats[] = $value->cat_ID;
}
echo "Cats: ".implode(',',$cats);
Will return Cats: 1,225,228,494,490,492,232,241,233,234,235,236,237,238,242
(作为管理员未过滤)。
当有限制的用户运行此脚本时,他们会得到类似 Cats: 1,225,228
的内容(但我需要列出所有类别)
如果我理解正确的话:
SELECT wp_terms.term_id
FROM wp_term_relationships
LEFT JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms on wp_term_taxonomy.term_taxonomy_id = wp_terms.term_id
WHERE wp_term_taxonomy.taxonomy = 'category'
GROUP BY wp_term_taxonomy.term_id
这 SQL 从数据库中获取所有分类法。希望对你有帮助
我需要什么以及为什么
由于我的插件 (WC_Membership) get_categories()
由于内容受限而被过滤。
现在我需要列出每个类别以进行比较(我会通过 get_categories()
这样做),但如果不是管理员,则不会列出所有类别。 SQL 是解决我的问题的方法吗? (不要使用 WP_Query -> 会被其他插件过滤)
TL;DR:我需要未过滤的类别 - 可能来自 SQL
通常我会...
$cats = [];
foreach (get_categories() as $key => $value) {
$cats[] = $value->cat_ID;
}
echo "Cats: ".implode(',',$cats);
Will return Cats: 1,225,228,494,490,492,232,241,233,234,235,236,237,238,242
(作为管理员未过滤)。
当有限制的用户运行此脚本时,他们会得到类似 Cats: 1,225,228
的内容(但我需要列出所有类别)
如果我理解正确的话:
SELECT wp_terms.term_id
FROM wp_term_relationships
LEFT JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms on wp_term_taxonomy.term_taxonomy_id = wp_terms.term_id
WHERE wp_term_taxonomy.taxonomy = 'category'
GROUP BY wp_term_taxonomy.term_id
这 SQL 从数据库中获取所有分类法。希望对你有帮助