简体   繁体   中英

Get all posts together with them tags and categories

I am new to wordpress and have found the function get_posts and it works fine but it does not retrieve the posts categories and tags . Is there another function to do it or the only way is to run a raw join against the db?

EDIT

This is what I have until now, but I get duplicated rows (1 per tag and 1 per category)

SELECT wp_posts.post_author, wp_posts.post_date, wp_posts.post_title, wp_posts.post_name, wp_terms.name, wp_users.display_name
FROM (wp_posts, wp_terms, wp_term_relationships)
INNER JOIN wp_users ON wp_users.ID = wp_posts.post_author
WHERE wp_posts.ID        = 314
AND wp_posts.ID          = wp_term_relationships.object_id
AND wp_terms.term_id     = wp_term_relationships.term_taxonomy_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type <> 'revision'

How should I get Categories and tags in only one row?

Thanks.

You're seeking for the class WP_Query.

The doc is here : https://codex.wordpress.org/Class_Reference/WP_Query

For category you can use get_the_category( int $id ) For tags you can use wp_get_post_tags( $post_id )

For reference : https://codex.wordpress.org/Function_Reference/wp_get_post_tags

https://developer.wordpress.org/reference/functions/get_the_category/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM