You’ve not joined the wp_terms
or $wpdb->terms
table where WordPress stores the term names. So here is the updated code-
$post_type_query = " AND post_type="" . $selected_post_type . """;
$posts_per_month = $wpdb->get_results(
"SELECT *, DATE_FORMAT(post_date, '%Y-%m') AS month, COUNT(ID) AS count
FROM {$wpdb->posts} AS wposts
LEFT JOIN {$wpdb->postmeta} AS wpostmeta ON (wposts.ID = wpostmeta.post_id)
LEFT JOIN {$wpdb->term_relationships} AS tax_rel ON (wposts.ID = tax_rel.object_id)
LEFT JOIN {$wpdb->term_taxonomy} AS term_tax ON (tax_rel.term_taxonomy_id = term_tax.term_taxonomy_id)
LEFT JOIN {$wpdb->terms} AS terms ON (terms.term_id = term_tax.term_id)
WHERE post_status="publish"" . $post_type_query .
"AND terms.name="business"
AND term_tax.taxonomy = 'your-custom-taxnomy-name'",
OBJECT_K
);
In above code put terms.name
value as your term’s name and terms_tax.taxonomy
value as your custom taxonomy name like category
or post_tag
. Here terms_tax.taxonomy
value is not mandatory, but it makes the result more precise.
Hope that helps.