• I need to query a custom post type with SQL to generate a XML file with all the post data. My query looks like this:

    SELECT  wp_posts.ID, wp_posts.post_title, wp_posts.post_name, pm1.meta_value as address,                 pm2.meta_value as lat, pm3.meta_value as lng, pm4.meta_value as homepage, pm5.meta_value as hots
    FROM wp_posts
    LEFT JOIN wp_postmeta AS pm1 ON (wp_posts.ID = pm1.post_id AND pm1.meta_key='Adresse')
    LEFT JOIN wp_postmeta AS pm2 ON (wp_posts.ID = pm2.post_id AND pm2.meta_key='Latitude')
    LEFT JOIN wp_postmeta AS pm3 ON (wp_posts.ID = pm3.post_id AND pm3.meta_key='Longitude')
    LEFT JOIN wp_postmeta AS pm4 ON (wp_posts.ID = pm4.post_id AND pm4.meta_key='website_link')
    LEFT JOIN wp_postmeta AS pm5 ON (wp_posts.ID = pm5.post_id AND pm5.meta_key='_zilla_likes')
    WHERE wp_posts.post_type = 'club'
    AND wp_posts.post_status = 'publish'
    AND ((pm1.meta_key = 'Adresse') OR (pm2.meta_key = 'Latitude') OR (pm3.meta_key = 'Longitude') OR     (pm4.meta_key = 'website_link') OR (pm4.meta_key = '_zilla_likes'))
    GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC;

    this returns a table of following format:

    | ID | post_title | post_name | address | lat | lng | homepage | hots |
    |----|------------|-----------|---------|-----|-----|----------|------|
    |    |            |           |         |     |     |          |      |
    |----|------------|-----------|---------|-----|-----|----------|------|

    But I don’t know how to get the category of each post, my goal is to get following table:

    | ID | post_title | post_name | address | lat | lng | homepage | hots | category |
    |----|------------|-----------|---------|-----|-----|----------|------|----------|
    |    |            |           |         |     |     |          |      |          |
    |----|------------|-----------|---------|-----|-----|----------|------|----------|

    I would be really happy if someone could give me an advice on how to adjust my query to get the second table. Thanks

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘How to query custom post category with SQL?’ is closed to new replies.