新的Wp_Query()或pre_get_posts()查看自定义帖子类型的所有帖子? [英] new Wp_Query() or pre_get_posts() to view ALL posts for custom post type?

查看:132
本文介绍了新的Wp_Query()或pre_get_posts()查看自定义帖子类型的所有帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存档模板文件,其中显示了所有自定义帖子类型人员的帖子(称为archive-personnel.php):



这是文件的开头...

 <?php 
get_header();

//显示此自定义帖子类型(人员)的所有帖子
$ args = array('post_type'=>'personnel','posts_per_page'=> -1);
$ personnel_query =新的WP_Query($ args);
if(have_posts()):而($ personnel_query-> have_posts()):$ personnel_query-> the_post();

这是可行的,但是我知道我可以使用 pre_get_posts()。但是使用 pre_get_posts()-过滤器,您必须检查它是否影响管理员等。



所以我的问题是: 我使用哪种替代方案真的很重要还是仅仅是偏好/口味的问题?

解决方案

Main 查询与 secondary 查询:




我使用哪种替代方法真的很重要吗还是仅仅是
偏好/口味的问题?


是,确实存在区别:



1) pre_get_posts正在修改主要查询(或所有查询),而不是使用WP_Query添加辅助查询。



2)如果您希望分页工作在辅助查询中,通常必须对主查询进行修改。。 p >

通过使用 pre_get_posts 挂钩,您可以修改作为 WP_Query()实例的所有查询,包括主查询,它是 WP_Query()的一个实例,并且在每个页面请求中都存在。



请记住 get_posts() WP_Query()的包装并且当 suppress_filters 属性设置为 FALSE 时,过滤器处于活动状态。



使用 pre_get_posts 时,您通常希望使用一些条件标记。以下是一些示例,您可以使用:



a) is_main_query() 以确定当前查询是否为 main 查询。



b) ! is_admin() 以防止在后端修改查询。



c) is_post_type_archive() 来定位帖子类型档案。



添加自定义 WP_Query()时,除了主查询之外,还会添加其他查询。



如果可以的话,我通常会执行 pre_get_posts 动作,而不是添加辅助查询。



示例:



如果要修改主查询,则对于自定义帖子类型为人员,您可以尝试:

  add_action('pre_get_posts',function($ query){ 
if(!is_admin()&& $ query-> is_main_query()){
if(is_post_type_archive('personnel')){
$ query-> set(' posts_per_page',-1);
}
}
});

这是未经测试的,但您会明白;-)



进一步阅读:



希望以下链接可以为您提供有关差异的更多信息:




I have an archive-template file that shows all posts for custom post type personnel (called archive-personnel.php):

This is the begining of the file...

<?php 
get_header(); 

//Show all posts for this custom posttype (personnel)
$args = array( 'post_type' => 'personnel', 'posts_per_page' => -1 );
$personnel_query = new WP_Query( $args );
if (have_posts()) : while ($personnel_query->have_posts()) : $personnel_query->the_post(); 

This is working, but I know that I can use pre_get_posts() as well. But with pre_get_posts() - filter you have to check if it affects admin etc.

So my question is: Does it really matter which alternative I use or is just a matter of preference/taste?

解决方案

Main query versus secondary queries:

Does it really matter which alternative I use or is just a matter of preference/taste?

Yes , there is a real difference:

1) pre_get_posts is modifying the main query (or all queries) versus adding a secondary query using WP_Query.

2) If you want paging to work on the secondary query, you usually have to make modifications to the main query.

By using the pre_get_posts hook you can modify all queries that are instances of WP_Query(), including the main query, which is an instance of WP_Query() and is present for every page request.

Remember that get_posts() is a wrapper for WP_Query() and the filter is active when the suppress_filters attribute is set as FALSE.

When using pre_get_posts you usually want to target specific queries by using some conditional tags. Here are few examples, you can use:

a) is_main_query() to determine if the current query is the main query.

b) ! is_admin() to prevent modifications to queries in the backend.

c) is_post_type_archive() to target the post type archives.

When you add your custom WP_Query(), then you are adding extra queries in addition to the main query.

I usually go with the pre_get_posts action if I can, instead of adding a secondary query.

Example:

If you want to modify the main query, for the archive of the custom post type personnel, you can try:

add_action( 'pre_get_posts', function( $query ){
  if ( ! is_admin() && $query->is_main_query() ) {
    if ( is_post_type_archive( 'personnel' ) ) {
      $query->set('posts_per_page', -1 );
    }
  }
});

This is untested but you get the idea ;-)

Further reading:

Hopefully the following links can give you more information regarding the difference:

这篇关于新的Wp_Query()或pre_get_posts()查看自定义帖子类型的所有帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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