In postmeta lets say

  1. meta_key = 'count' and meta_value = '5'
  2. meta_key = 'count' and meta_value = '6'
  3. meta_key = 'count' and meta_value = '7'

now i want to get all post

WHERE meta_key = 'count'

AND orderby = 'meta_value_num'

AND order by ASC

NOTE: I dont wanna use query_posts or wp_query. just custom sql query.

  • Is there any particular reason why you wouldn't use WP_Query for that? There's the nice meta_query parameter that you can use. Otherwise It's not really a WordPress question because you basically need to inspect the WP database structure and construct a custom SQL query. You are getting downvoted because most users here advocate for doing everything according to standards and using WordPress' API where possible. – Mateusz Hajdziony Nov 1 '15 at 15:47

While you might not want / be able to use native query, it is still the easiest and most reliable way to generate SQL like that.

$wpdb->last_query will remember last query performed and defining SAVEQUERIES constants will make $wpdb->queries contain all of the queries performed in current load (not advisable in production for performance).

So expressing desired query via API and looking at SQL that was generated for it is the best way to go about it.

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.