How does Wordpress link posts to categories in its database?

后端 未结 1 2057
北海茫月
北海茫月 2021-02-04 07:20

At present I am displaying a list of the last 5 posts in a site\'s blog in its footer using this mysql query:

SELECT post_title, guid, post_date FROM wp_posts WH         


        
相关标签:
1条回答
  • 2021-02-04 07:40

    The relations of the Wordpress database is available in the database diagram.

    In your particular case it is:

    wp_posts.ID
    ->wp_term_relationships.object_id
    ->wp_term_relationships.term_taxonomy_id
    ->wp_term_taxonomy.term_taxonomy_id
    ->wp_term_taxonomy.term_id
    ->wp_terms.term_id

    For querying you need to use an SQL join:

    SELECT p.ID, t.term_id
    FROM wp_posts p
    LEFT JOIN wp_term_relationships rel ON rel.object_id = p.ID
    LEFT JOIN wp_term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id
    LEFT JOIN wp_terms t ON t.term_id = tax.term_id
    

    But it should be noted that the wordpress database might change at any time, and you should use the Wordpress provided mechanisms (such as query_posts) to filter posts from the database.

    0 讨论(0)
提交回复
热议问题