【问题标题】:How to get Wordpress categories (the_categories()) via DB (SQL)如何通过 DB (SQL) 获取 Wordpress 类别 (the_categories())
【发布时间】:2021-03-23 07:40:26
【问题描述】:

我需要什么以及为什么

由于我的插件 (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);

将返回Cats: 1,225,228,494,490,492,232,241,233,234,235,236,237,238,242(未过滤为管理员)。

当有限制的用户运行此脚本时,他们会得到类似Cats: 1,225,228 的信息(但我需要列出所有类别)

【问题讨论】:

    标签: php sql wordpress plugins


    【解决方案1】:

    如果我正确理解你的话:

    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 从数据库中获取所有分类。希望能帮到你

    【讨论】:

    • 工作顺利,但它返回 nice_names 而不是 ID。我试图用SELECT wp_terms.ID 交换SELECT wp_terms.name,但它仍然没有返回ID。您能否编辑您的答案以提供 ID。谢谢大家!
    猜你喜欢
    • 1970-01-01
    • 2017-04-23
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多