Support » Fixing WordPress » List posts taxonomies without knowing what they are in advance

  • Resolved Dale Sattler

    (@noponies)


    I’ve built a Actionscript – AMF – PHP – WordPress bridge, that lets Flash use WordPress as a CMS.

    When building what I call a PostVO object, (which is basically a representation of a post within the database with some extra values added on. Which are categories, post_tags, attachments, featured image and permalink) I’ve come across a slight hurdle with getting a list of the taxonomies the post belongs within. I can access them via get_post_taxonomies which gives me a list of possible taxonomies, but not the particular ones associated with a particular post.

    get_post_taxonomies is a start, and I can run a for each loop against what this returns, using wp_get_object_terms to check if the post has that particular taxonomy.

    But it seems a little inefficient. I can access each posts taxonomies via a SQL query. Ideally however I try to resist working directly on the database and prefer to use the WordPress API.

    So, is there a function I can call that returns the taxonomies of a particular post, without knowing what those taxonomies are in advance?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Heres the basic SQl query;

    SELECT taxonomy
    FROM wp_terms
    INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
    INNER JOIN wp_term_relationships  wpr ON wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    INNER JOIN wp_posts p  ON p.ID = wpr.object_id
    AND p.post_type =  $postType
    AND p.ID = $postId
    ORDER BY object_id

    In the end I just used that SQL query to return an array of taxonomy names, which I pass to get_the_terms.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘List posts taxonomies without knowing what they are in advance’ is closed to new replies.