• Resolved denhartenweg

    (@crazyhero)


    The thread mysql query for all posts in “news” category? is closed so I post my addition here:

    In my case the term_taxonomy_id was not the same as the term_id so there needed to be an extra LEFT OUTER JOIN and a change to the second LEFT OUTER JOIN. Otherwhise it shows wrong posts.

    The old solution

    SELECT * FROM wp_posts p
    LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
    LEFT OUTER JOIN wp_terms t ON t.term_id = r.term_taxonomy_id
    WHERE p.post_status = 'publish'
    AND p.post_type = 'post'
    AND t.slug = 'news'

    New solution for situations where term_id != term_taxonomy_id

    SELECT * FROM wp_posts p
    LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
    LEFT OUTER JOIN wp_term_taxonomy x ON x.term_taxonomy_id = r.term_taxonomy_id
    LEFT OUTER JOIN wp_terms t ON t.term_id = x.term_id
    WHERE p.post_status = 'publish'
    AND p.post_type = 'post'
    AND t.slug = 'news'

    Now it wors like a charm. I hope this one helps somebody.

  • The topic ‘mysql query for all posts in “news” category? – id problem’ is closed to new replies.