0

enter image description hereI'm trying to query all posts from my Wordpress site that have wp_terms.slug = 'portfolio-photos'. I've connected the tables using wp_terms, wp_term_taxonomy, wp_term_relationships and wp_posts. I'm not sure what's wrong with the SQL code I have. Can someone advise what's wrong with my code/train of thought on this?

I receive 0 rows, however, there should be 12 rows because that's how many I have in WP with the "portfolio-photos" slug. The column headers are ID, term_id, and slug.

SELECT wp_posts.ID, wp_terms.term_id, wp_terms.slug
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
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_terms.slug = 'portfolio-photos'
6
  • are you trying to fetch the result using PHP ? or just mysql? Commented May 2, 2020 at 21:06
  • Just in the php admin using SQL Commented May 2, 2020 at 21:17
  • php admin or php my admin? Commented May 2, 2020 at 21:20
  • In My php admin Commented May 2, 2020 at 21:26
  • your statement is still vauge, could you please tell me if you're trying to complete this task using the "php my admin" or you've developed an admin panel using php and you want to show it there. Thanks Commented May 2, 2020 at 21:28

2 Answers 2

0

The table wp_terms is nowhere in your FROM clause, you'd have to relate it to use it in your WHERE clause.

EDIT: Original post was changed after my original answer.

3
  • Hello Khoa, I updated my question to reflect the connection to wp_terms by using wp_term_relationships and wp_term_taxonomy but now I receive 0 results Commented Apr 30, 2020 at 6:25
  • I noticed you just updated your question, now it seems maybe your JOINS are what's causing the 0 results. Without knowing the table structures, it's hard to tell.
    – Khoa
    Commented Apr 30, 2020 at 6:29
  • Here's a good read on JOINS if you want to peruse through it, techonthenet.com/mysql/joins.php
    – Khoa
    Commented Apr 30, 2020 at 6:38
0

I realized I had the correct code after carefully looking at how the tables in the WP DB relate. This article really helped. I was trying to pull up results using a slug that corresponded with the parent category.

Therefore, my original code above only worked for sub-categories. This code below pulls up the correct amount of results by using WHERE wp_term_taxonomy.parent = '11'

SELECT wp_posts.ID, wp_terms.term_id, wp_term_taxonomy.parent, wp_terms.slug
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
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.parent = '11'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.