How to return the SQL Query code from any View?

I have been struggling for a few days after this

I need to return the SQL Query of a given view or from the current page

I finally figured out how, it is quite simple with only 2 lines of code:

$view = views_get_view('nomedaview'); // or views_get_page_view() for the current page view

$query_string = (string) $view->build_info[&query];

That's all !!!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Twitter-style #hashtags are linked to https://twitter.com.
  • Twitter-style @usernames are linked to their Twitter account pages.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <bash> <php> <css> <mysql>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.