I hope someone will help me.............

I have a

  • Post Type ----> Dealers
  • Taxonomy ----> State

how to write a sql query in this format

$catinfo1 = $wpdb->get_results("select t.*,(select count(tr.object_id) from $wpdb->term_relationships tr join $wpdb->posts p on p.ID=tr.object_id where tt.term_taxonomy_id=tr.term_taxonomy_id and p.post_status='publish') pcount from $wpdb->terms t join $wpdb->term_taxonomy tt on tt.term_id=t.term_id where tt.taxonomy=\"fueltype\" and tt.parent=0 order by t.name");
  • this kinds of question resolved before. Check this one wordpress.stackexchange.com/questions/15040/… – Sisir May 14 '12 at 10:28
  • @Sisir thanks for the response, i appreciate you :) the solution u gave is like " Showing posts by collection of specific terms and taxonomy " as i have to write for all the terms and maybe later more terms will be added based on the requirement...... can u help me how to write it using $wpdb.. – Solomon Henry May 14 '12 at 10:34
  • 1
    Solomon, I would advise against writing is as a raw SQL statement, you're bypassing all the caching filtering and sanitisation WP normally does in order to do it yourself. This would be more work than what Sisir is suggesting – Tom J Nowell May 14 '12 at 10:39
  • If querying multiple terms in a taxonomy is what you're after instead of a single term, then you should still use WP_Query, look up the tax query section in the codex – Tom J Nowell May 14 '12 at 10:40

@Sisir linked you to the appropriate place, if you had read the question/answer and the linked Codex documentation you'd have seen that you could do something like the following:

$args = array(
    'post_type' => 'dealers'
    'tax_query' => array(
            'taxonomy' => 'state',
            'field' => 'slug',
            'terms' => array('bob','angela','john','smith','jan','doe','etc...')
$query = new WP_Query( $args );

Note the multiple state terms being queried, not the single specific one.

|improve this answer|||||
  • Thanks and point noted........ as i can see " array('bob','angela','john','smith','jan','doe','etc...') " we r passing the values static, can it be done dynamically, sorry... kindly bare with me... – Solomon Henry May 14 '12 at 10:47
  • Yes it's expecting an array of terms, how the array is defined is up to you. I hardcoded it here for the purposes of demonstration, but you could pass it in as a variable if you wanted etc – Tom J Nowell May 14 '12 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.