简体   繁体   中英

Trying to select and count all the matching terms in my wp_post and wp_term_relationship

I'm trying to make a query that looks sort of like this:

what I'm trying to accomplish

getting a count of each term_taxonomy_id that corresponds to a wp_post.ID and wp_term_relationship . So, I can display a count for each where the desired count value corresponds to that respective wp_term_relationship.term_taxonomy_id . So, I wrote the following test query and didn't have any luck:

$db_query = $wpdb->get_results("SELECT wp_post.ID, wp_term_relationship.object_id FROM wp_term_relationships INNER JOIN wp_posts ON wp_posts.ID=wp_term_relationship.object_id");

I know this wouldn't receive the count but am I at all on the right track here. If you need anymore information let me know and I will update according.

Sample data

wp_posts table -> ID, post_author, etc.. I'm only concerned about the ID attribute from the posts table wp_term_relationship -> object_id, term_taxonomy_id im only really concerned about term_taxonomy_id here.

you have enter wrong table name "wp_term_relationship" its "wp_term_relationships". run below query .

$db_query = $wpdb->get_results("SELECT wp_posts.ID, wp_term_relationships.object_id FROM wp_term_relationships INNER JOIN wp_posts ON wp_posts.ID=wp_term_relationships.object_id");

tell me if there is any problem

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