HamSter007
@HamSter007
HTML/CSS верстальщик

В чем отличие query_posts от new WP_Query?

Есть ли отличия в записи:

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


ОТ

<?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(); ?>


И какую все же лучше использовать?
  • Вопрос задан
  • 365 просмотров
Решения вопроса 1
link_web
@link_web
Magento, Laravel, Zend, Shopify, Prestashop, WP
Не используйте query_posts, он повторно запускает запросы в базу и переопределяет глобальный объект WP_Query . Из офф. документации:
Note: This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible. In most cases, there are better, more performant options for modifying the main query such as via the ‘pre_get_posts’ action within WP_Query.

Используйте get_posts или pre_get_posts - Это обертка WP_Query , которая упрощает выборку постов и не изменят глобальные переменные, в ней еще определены некоторые дефолтные аргументы из-за чего выборку сделать проще.

Для более гибкой выборки используйте WP_Query
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы