0

I have very complex wp query and I want to use sql query instead of this. Is there any way to translate wp query to sql query. I am thinking that I will have a better control over sql query as the wp query is not fulfilling my requirements.

  • You can use $wpdb->prepare to run your custom SQL Query. Please check these links: developer.wordpress.org/reference/classes/wpdb/prepare & codex.wordpress.org/Class_Reference/wpdb – Bhupen Jun 13 at 13:13
  • Okay but is there any way to translate existing wp query to sql query automatically ? – wplearner Jun 13 at 13:22
  • 2
    What's the context behind translating it into SQL? What are the requirements you're needing to add? WP_Query might be able to fulfill them if you share what they are via filters or options you might be unaware of. It could even be that the way your storing things could be improved to be faster and easier to query. Note that by using raw SQL you give up any benefits of WP Cache, object caching, and caching plugins – Tom J Nowell Jun 13 at 13:24
  • Technically there is no way to do that. At the very least you are dealing with two tables viz _posts and _postmeta. You can try out creating a local clone of the site(or the database if you just want to play with sql queries and form one to your liking) and test them out in a client like phpmyadmin. – Faham Shaikh Jun 13 at 13:25
  • Posting your query here might be helpful and as @TomJNowell mentioned you give up on significant benefits by using direct sql statement. – Faham Shaikh Jun 13 at 13:27

Your Answer

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

Browse other questions tagged or ask your own question.