I’m trying to make a wpdb query that returns all posts whose titles begin with a certain letter, and belong to a certain category. So far, I have this:
$answer = $wpdb->get_results("SELECT * FROM wp_posts WHERE SUBSTRING(post_title,1,1)='T' AND post_type='post'");
This returns ALL posts that begin with the letter T. This is good, however, I want to be more narrow than that. I want only those belonging to a certain category ID to show.
The category ID does not seem to be a field in the wp_posts table. How do I access it and insert it in my SQL statement?
——-EDIT—–
Nevermind, figured it out.
It’s:
$answer = $wpdb->get_results("SELECT post_title, post_content, term_taxonomy_id FROM wp_posts LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id WHERE SUBSTRING(post_title,1,1)='T' AND term_taxonomy_id=6");
1 comment