H
H
HamSter2018-07-03 17:46:07
WordPress
HamSter, 2018-07-03 17:46:07

What is the difference between query_posts and new WP_Query?

Are there any differences in the entry:

<?php
query_posts('post_type=services&posts_per_page=-1' );
  if (have_posts()) : while (have_posts()) : the_post();
    ...

FROM
<?php 

  $args = array('post_type' => 'services', 'posts_per_page' => '-1');
  $the_query = new WP_Query( $args ); ?>

    <?php if ( $the_query->have_posts() ) : ?>
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

And which one is better to use?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
imdeveloper, 2018-07-03
@HamSter007

Don't use query_posts,it re-runs queries against the database and overrides the global WP_Query. From off. documentation:
Use get_posts или pre_get_posts- This is a wrapper WP_Querythat simplifies the selection of posts and will not change global variables, it still defines some default arguments, which makes it easier to select.
For a more flexible selection, useWP_Query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question