MySQL query-uri pentru WordPress

Lucrez cu WordPress de cativa ani, cam de cand am blog-ul. Intodeauna mi-a placut sa imi bag mana prin script-uri si sa scot ce este mai bun fara a ma limita la ce-mi ofera el sau plugin-uri gasite pe internet. Asa am inceput sa modific teme si plugin-uri pentru a le ajusta dupa nevoile mele. Intre timp am inceput sa-i ajut si pe altii care se loveau de fel si fel de probleme.

Invatam cat traim, internetul iti pune pe tava atatea informatii, trebuie doar sa stii sa le gasesti si sa te folosesti de ele. Au fost situatii cand a trebuit sa “leg” un site de un blog, singura punte de legatura fiind baza de date, acolo unde sunt stocate toate informatile. Asa am inceput sa invat cum sa introduc, extrag sau modific informatii din tabelele folosite de WordPress.

In exemplele de mai jos se presupune ca sunteti cat de cat familiarizati cu WordPress si baze de date MySQL. Mi-am dorit sa am o lista cu query-uri utile undeva, de ce sa muncesc de fiecare data sa le rescriu cand le-am folosit deja candva. Mentionez ca n-am folosit alias-uri, dar puteti scurta query-urile singuri. 🙂

1. resetarea parolei la un cont fara a fi nevoie de adresa de email

UPDATE wp_users
SET user_pass = MD5('NOUA-PAROLA')
WHERE ID = '1'
LIMIT 1

2. extragerea atasamentelor de la post-uri, doar imaginile

SELECT post_title, guid
FROM wp_posts
WHERE post_status = 'inherit'
AND post_type = 'attachment'
AND SUBSTR(post_mime_type,1,5) = 'image'
ORDER BY post_date DESC

3. curatarea tabelei cu post-uri de informatii nefolositoare, dar care se strang inutil in tabela

DELETE FROM wp_posts
WHERE post_type IN ('revisions','auto-draft')

4. inchiderea posibilitatii de a comenta la post-uri mai vechi de 90 de zile

UPDATE wp_posts 
SET comment_status = 'closed'
WHERE post_date < DATE_ADD(NOW(), INTERVAL -90 DAY) 
AND post_status = 'publish'

5. atribuie toate post-urile scrise de un user catre altul

UPDATE wp_posts 
SET post_author = '2' 
WHERE post_author = '1'

6. lista de adrese unice de email ale persoanelor care au lasat unul sau mai multe comentarii pe blog

SELECT DISTINCT comment_author_email 
FROM wp_comments
ORDER BY comment_author_email ASC

7. lista de adrese unice de email ale persoanelor care au lasat unul sau mai multe comentarii la un post

SELECT DISTINCT comment_author_email 
FROM wp_comments
WHERE comment_post_ID = 'ID-POST'

8. lista de post-uri dupa ID-ul unui tag

SELECT wp_posts.post_title, wp_posts.guid
FROM wp_posts
INNER JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE wp_term_taxonomy.taxonomy = 'post_tag'
AND wp_term_taxonomy.term_id = '85'
AND wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC
LIMIT 0,7

9. lista de post-uri dupa ID-ul mai multor tag-uri

SELECT wp_posts.post_title, wp_posts.guid
FROM wp_posts
INNER JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE wp_term_taxonomy.taxonomy = 'post_tag'
AND wp_term_taxonomy.term_id IN ('ID-TAG-1','ID-TAG-2','ID-TAG-3)
AND wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC
LIMIT 0,7

10. lista de post-uri dintr-o categorie pe baza ID-ului categoriei

SELECT wp_posts.post_title, wp_posts.guid
FROM wp_posts
INNER JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE wp_term_taxonomy.taxonomy = category
AND wp_term_taxonomy.term_id = 'ID-CATEGORIE'
AND wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC
LIMIT 0,7

11. lista de tag-uri neatribuite la post-uri

SELECT wp_terms.term_id, wp_terms.name, wp_terms.slug 
FROM wp_terms
INNER JOIN wp_term_taxonomy 
ON wp_terms.term_id = wp_term_taxonomy.term_id 
WHERE wp_term_taxonomy.taxonomy = 'post_tag'
AND wp_term_taxonomy.count = '0'

12. stergerea tuturor tag-urilor neatribuite post-urilor

DELETE wp_terms.*, wp_term_taxonomy.*
FROM wp_terms
INNER JOIN wp_term_taxonomy 
ON wp_terms.term_id = wp_term_taxonomy.term_id 
WHERE wp_term_taxonomy.taxonomy = 'post_tag'
AND wp_term_taxonomy.count = '0'

Stiu ca lista cuprinde doar cateva query-uri, acestea pot fi ajustate dupa nevoile fiecaruia. Aveti sugestii de query-uri pe care le-am omis?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.