简体   繁体   中英

Get a list of the last posts grouped by author and filtered by category

i need a list of the latest post published by various authors, but filtered by category, like for example the latest posts of author that write about music.

i wrote this query and looks like working in mysql

SELECT * FROM
(
    SELECT * 
    FROM $wpdb->posts
    INNER JOIN 
        wp_term_relationships ON $wpdb->posts.ID = wp_term_relationships.object_id
    WHERE 
        wp_term_relationships.term_taxonomy_id = 7
        AND
        $wpdb->posts.post_status = 'publish'

    ORDER BY $wpdb->posts.post_date DESC
)
AS x GROUP BY post_author

(the 7 is just an example category id)

i just would like to know the other $wpdb functions to call the wp_term_relationships table and if is there something wrong with this query

thank you in advance

How about

$args = array(
    'post_type' => 'post',
    'cat' => $category_id,
);

$wp_query = new WP_Query($args);

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