$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - How to print raw query from WP_Query class just like in CodeIgniter|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - How to print raw query from WP_Query class just like in CodeIgniter

matteradmin10PV0评论

I am struggling with WordPress, and was looking at WP_Query. We usually pass an array of arguments to get result against.

$args = array(
        'post_type' => 'post',
        'post_per_page' => '2',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'ignore_sticky_posts' => true
    );

$the_query = new WP_Query( $args );

Is there any way to print out $the_query in raw form for testing purpose just like we do in CodeIgniter with $this->db->last_query();?

Raw Query Example:

select * from table1 where ......

I am struggling with WordPress, and was looking at WP_Query. We usually pass an array of arguments to get result against.

$args = array(
        'post_type' => 'post',
        'post_per_page' => '2',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'ignore_sticky_posts' => true
    );

$the_query = new WP_Query( $args );

Is there any way to print out $the_query in raw form for testing purpose just like we do in CodeIgniter with $this->db->last_query();?

Raw Query Example:

select * from table1 where ......
Share Improve this question edited Jul 13, 2017 at 16:27 Morgan Estes 1,55512 silver badges22 bronze badges asked Jul 13, 2017 at 14:17 mohsinmohsin 1772 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

The generated SQL is available via the request property:

echo $the_query->request;

where $the_query is a \WP_Query instance.

Check out how it's formed in the class here.

Also available via the posts_request filter for unsuppressed filtering.

Post a comment

comment list (0)

  1. No comments so far