1

I'm trying to query posts from the WP database with the category uncategorised, while also joining the post meta table, but I can't get it to work. I get an empty result.

This is my query

SELECT * FROM wptests_posts wp
INNER JOIN wptests_postmeta wm ON (wm.`post_id` = wp.`ID`)
INNER JOIN wptests_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wptests_term_taxonomy wtt ON (wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`)
INNER JOIN wptests_terms wt ON (wt.`term_id` = wtt.`term_id`) AND
((wtt.`taxonomy` = 'category' AND  wt.`slug` = 'uncategorized'))

It seems to be the postmeta inner join on line 2. If i remove it, I get the expected results, except I don't then get the post meta.

Any help is appreciated

  • 1
    Why are you not using WP_Query – Pieter Goosen Nov 10 '15 at 18:30
  • Long story short: I'm developing a library, it needs to be a MySQL query – SomeNorwegianGuy Nov 10 '15 at 19:31
  • In my installation, default uncategorized category uses "uncategorised" slug, not "uncategorized". Can you check that? I've tested your code changing the slug value and it works perfectly. – cybmeta Nov 10 '15 at 20:34
  • I did not hardcode that slug, it is retrieved by getting the slug of the category term with id=1. But i tried anyway, didn't work. – SomeNorwegianGuy Nov 12 '15 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.