应该避免使用query_posts()吗? [英] query_posts() should be avoided?

查看:105
本文介绍了应该避免使用query_posts()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到应该避免使用query_posts(),而推荐使用wp_query()pre_get_posts().我对弄乱Loop并没有信心,也没有完全理解编解码器.

I am reading that query_posts() should be avoided in favor of wp_query() and pre_get_posts(). I am not confident with messing with the Loop and do not fully understand the codex.

下面的代码是否使用query_posts()?如果是,并且由于应避免使用query_posts(),您是否可以建议一种不使用query_posts()但仍然完成相同操作的方法?

Does the code below use query_posts() ? If yes and since query_posts() should be avoided, can you suggest a method that does not use query_posts() but still accomplish the same thing?

functions.php中的此代码用于按随机或按价格对帖子进行排序.

This code in functions.php is used to sort posts by random or by price.

function my_custom_query($query){
 if ( $query->is_home() && $query->is_main_query() ) {

   $sort= $_GET['sort'];

   if($sort == "pricelow"){
     $query->set( 'meta_key', 'price' );
     $query->set( 'orderby', 'meta_value_num' );
     $query->set( 'order', 'ASC' );
    }

  if($sort == "random"){
     $query->set( 'orderby', 'rand' );
    }

 }
}
add_action( 'pre_get_posts', 'my_custom_query' );

.
使用此代码将链接A(随机)和链接B(价格)发布在我的菜单中.因此,网站的访问者只需单击链接即可对帖子进行排序.

.
Link A (Random) and Link B (Price) are posted in my menu by using this code. Thus the visitor to the website can sort the posts simply by clicking a link.

<a href="http://website.com/?sort=A">Random</a>
<a href="http://website.com/?sort=B">Price</a>

推荐答案

我已经就WPSE这个主题做了非常详细的解释,为了对SO用户可能具有价值和益处,这里是从该问题在WPSE上复制的完整帖子.出于兴趣的考虑,下面是指向WPSE上完整文章的链接:对于主查询和自定义查询的工作方式有些疑问这个自定义主题?

I have done a very detailed explanation on this very topic on WPSE, and for the sake of the value and benefit it might have for SO users, here is the complete post copied from that question on WPSE. For interest sake, here is a link to the complete post on WPSE: Some doubts about how the main query and the custom query works in this custom theme?

您的实际问题基本上是何时运行自定义查询以及何时使用主查询.让我们将其分为三个部分

Your actual question is basically when to run a custom query and when to make use of the main query. Lets break it down in three parts

PART ONE

何时运行自定义查询(这不是确定的列表)

When to run a custom query (This is not a definitive list)

  • 创建自定义内容滑块

  • To create custom content sliders

在页面中创建特色内容区域

To create a featured content area in a page

如果需要显示帖子,请在page.php模板上

On page.php templates if you need to display posts

如果您需要在静态首页上自定义内容

If you require custom content on a static front page

显示相关,热门或信息性帖子

Display related, popular or informational posts

主查询范围之外的任何其他辅助或补充内容

Any other secondary or supplementary content outside the scope of the main query

何时使用主查询.

要在其上显示主要内容

  • 在您的主页上,并将该页面设置为后端中的博客页面

  • On your homepage and the page set as a blogpage in the backend

所有存档页面,其中包括诸如archive.php,category.php,author.php,taxonomy.php,tag.php和date.php之类的模板

All archive pages which includes templates like archive.php, category.php, author.php, taxonomy.php, tag.php and date.php

两个部分

要选择所有特色帖子,我使用以下行来创建一个新的WP_Query对象,该对象定义了具有特定标签特色的查询:

To select all the featured posts I use this line that create a new WP_Query object that define a query having the specific tag featured:

因此,据我了解,这不是WordPres主查询,而是由我创建的新查询.据我了解,最好是创建一个新查询(完成),而当我要执行这种操作时不使用主查询

So, from what I have understand, this is not the WordPres main query but it is a new query created by me. From what I have understand it is better create a new query (as done) and not use the main query when I want perform this kind of operations

正确.这超出了主查询的范围.这是辅助查询或补充内容,无法使用主查询创建.您总是应该使用 WP_Query get_posts 来创建您的自定义查询.

Correct. This falls out of scope for the main query. This is secondary or supplementary content which cannot be created with the main query. You SHOULD ALWAYS use either WP_Query or get_posts to create your custom queries.

从不使用 query_posts 来创建自定义查询,甚至其他任何查询.我的重点.

NEVER USE query_posts to create custom queries, or even any other query. My emphasis.

注意:此功能并非供插件或主题使用.如稍后所述,有更好的,性能更高的选项可以更改主查询. query_posts()是通过将页面的主查询替换为新的查询实例来简化页面主查询的过于简单和成问题的方式.它效率低下(重新运行SQL查询),并且在某些情况下(特别是在处理帖子分页时,通常会完全失败).

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

继续前进

好吧,继续,我显示所有没有功能标签的帖子,为此,我使用以下代码片段,相反地,它修改了主查询:

Ok, going on I show all the posts that have not the featured tag, to do this I use this code snippet that on the contrary modify the main query:

query_posts( array( 'tag__not_in' => array ( $term->term_id )));

所以我认为这太可怕了.是真的吗?

So I think that this is pretty horrible. Is it true?

那是完全错误的,不幸的是您的陈述是正确的.如前所述,从不使用query_posts.它会运行一个全新的查询,这对性能不利,并且在大多数情况下会破坏分页,这是使分页正常工作的主要查询的重要组成部分.

That is all wrong and your statement is unfortunately true. As said before, NEVER use query_posts. It runs a complete new query, which is bad for performance, and it most cases breaks pagination which is an integral part of the main query for pagination to work correctly.

这是您的主要内容,因此您应该使用具有默认循环的主查询,该循环看起来应该像这样,这就是您所需要的

This is your primary content, so you should be using the main query with the default loop, which should look like this, and this is all you need

<?php
    if (have_posts()) :
        // Start the Loop.
        while (have_posts()) : the_post();
get_template_part('content', get_post_format());

        endwhile;
    else :
        // If no content, include the "No posts found" template.
        get_template_part('content', 'none');

    endif;
?>

您可以完全摆脱这一部分,将其删除,刻录并忘掉它

You can completely get rid of this part, delete it, burn it and forget about it

<?
// get the term using the slug and the tag taxonomy
$term = get_term_by( 'slug', 'featured', 'post_tag' );
// pass the term_id to tag__not_in
query_posts( array( 'tag__not_in' => array ( $term->term_id )));
?>

好的,一旦完成,您将看到来自feature标签的帖子使用主查询和默认循环显示在您的主页中.

OK, once you've done that, you'll see that posts from the feature tag appear in your home page using the main query and default loop.

从首页中删除此标记的正确方法是使用 pre_get_posts .这是更改主要查询的正确方法,您应该始终使用钩子来更改主内容循环.

The correct way of removing this tag from the homepage is with pre_get_posts. This is the proper way to alter the main query and the hook you should always use to make changes to your primary content loop.

因此,带有pre_get_posts的代码是正确的,这是您应该使用的功能.只是一件事,请务必检查您是否不在管理页面上,因为pre_get_posts也会更改后端.因此,这是在functions.php中使用的正确代码,可从首页中删除标记为 featured 的帖子

So, the code with pre_get_posts is correct and this is the function that you should use. Just one thing, always do a check that you are not on an admin page because pre_get_posts alters the back end as well. So this is the proper code to use in functions.php to remove posts tagged featured from the homepage

function exclude_featured_tag( $query ) {
    if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
        $query->set( 'tag__not_in', 'array(ID OF THE FEATURED TAG)' );
    }
}
add_action( 'pre_get_posts', 'exclude_featured_tag' );

三部分

额外的阅读材料,将来会有所帮助

Extra reading material which will be helpful in future

何时应使用WP_Query与query_posts()与get_posts()?

何时使用WP_query(),query_posts()和pre_get_posts

查询概述

使用CMS循环指南

这篇关于应该避免使用query_posts()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆