Initiating WP_Query() with no args and adding them later through the query() method

I've inherited maintenance of a theme from another developer, and I'm trying to understand if there's a g

I've inherited maintenance of a theme from another developer, and I'm trying to understand if there's a good reason why they did this.

The theme includes a page template that runs a custom query for posts. It's set up like this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query = new WP_Query();
    $query->query('posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);

What I do, and what I've always seen others do, is pass in the query arguments while creating the new class instance, i.e. new WP_Query($args);

Does calling the $query->query($args) method do anything different? Is it identical from a performance perspective? (I want to make sure it's not hitting the database twice.)

I've inherited maintenance of a theme from another developer, and I'm trying to understand if there's a good reason why they did this.

The theme includes a page template that runs a custom query for posts. It's set up like this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query = new WP_Query();
    $query->query('posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);

What I do, and what I've always seen others do, is pass in the query arguments while creating the new class instance, i.e. new WP_Query($args);

Does calling the $query->query($args) method do anything different? Is it identical from a performance perspective? (I want to make sure it's not hitting the database twice.)

Share Improve this question asked May 16, 2019 at 22:14 AndronAndron 1536 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

Does calling the $query->query($args) method do anything different?

$query = new WP_Query( $args ); is simply a shortcut to the "instantiate class first, query later" version like $query = new WP_Query; $posts = $query->query( $args );. You can see it from the class constructor source here:

public function __construct( $query = '' ) {
    if ( ! empty( $query ) ) {
        $this->query( $query );
    }
}

Is it identical from a performance perspective? (I want to make sure it's not hitting the database twice.)

Yes, unless of course you do something like this:

$args = [ 'paged' => 2 ];
$query = new WP_Query( $args );
$posts = $query->query( $args );

Then that means double queries.

However, there's a difference in the return value:

$query = new WP_Query( $args );  // returns an object
$posts = $query->query( $args ); // returns an array

And get_posts() (source) for example, uses the second approach.

I'm trying to understand if there's a good reason why they did this

I don't know why did they do it that way, but it is perfectly fine (although the code would be a few bytes more compact when using direct query upon instantiating the class). And I'd concern more on using the proper query parameters; e.g. using the proper post type, and setting no_found_rows to true when you don't need pagination — in fact, setting it to true can improve performance.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745485723a4629757.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信