La relazione tra articoli e categorie è mantenuta dalla tabella wp_term_relationships. Questa è una tabella per la definizione di una relazione molti a molti tra le tabelle wp_term_taxonomy e wp_posts.
- object_id è la chiave d’accesso alla tabella wp_posts,
- term_taxonomy_id è l’identificativo d’accesso alla tabella wp_term_taxonomy.
select * from wp_term_relationships where term_taxonomy_id in ( select taxonomy.term_taxonomy_id from wp_terms terms left join wp_term_taxonomy taxonomy on terms.term_id = taxonomy.term_id where taxonomy.taxonomy = 'category' and terms.name = 'Mia Categoria');
oppure per proporla in una forma più corretta:
select relationships.object_id from wp_terms terms left join wp_term_taxonomy taxonomy on terms.term_id = taxonomy.term_id left join wp_term_relationships relationships on taxonomy.term_taxonomy_id = relationships.term_taxonomy_id where taxonomy.taxonomy = 'category' and terms.name = 'Mia categoria';
Quindi abbiamo tutti gli identificativi dei post legati ad una precisa categoria.
Ne viene di conseguenza la query per aggiornare lo stato degli articoli in bozza (‘draft’):
update wp_posts set post_status = 'draft' where ID in ( select relationships.object_id from wp_terms terms left join wp_term_taxonomy taxonomy on terms.term_id = taxonomy.term_id left join wp_term_relationships relationships on taxonomy.term_taxonomy_id = relationships.term_taxonomy_id where taxonomy.taxonomy = 'category' and terms.name = 'Mia categoria');