questions

How to get WP categories (the_categories()) via DB (SQL)

Rate this post

What I need and why

Because of my plugins (WC_Membership) get_categories() is filtered due to restricted content.

Now I need every category listed for comparison reasons (which I would do via get_categories()), but not all categories are listed if not admin. Is SQL the way to go for my problem? (Dont use WP_Query -> would get filtered by the other plugin)

TL;DR: I need unfiltered categories – probably via SQL

Normaly I do …

$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 (unfiltered as admin).

When a user with restrictions runs this script, they would get something like Cats: 1,225,228 (but I need all categories listed)

 

✔️Solution:

If I correct understand you:

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

This SQL get all taxonomies from database.

Leave a Reply

Your email address will not be published.

Back to top button