WP搜索页面posts根据post类型计数条件显示
WP Search page posts count conditional display based on post type
我正在尝试根据 post 类型在搜索页面上实现 post 数量(post 计数)的条件显示。
我正在寻找这样的东西:
add_action( 'pre_get_posts', function ( $query ) {
if( ! is_admin() && $query->is_search() && $query->is_main_query() && is_post_type == 'MY_POST_TYPE' ) {
$query->set( 'posts_per_page', 1 );
};
} );
我可以在哪里使用 is_post_type == 'MY_POST_TYPE'
来指定 post 类型...我一直在四处寻找但无法找到答案...这有点麻烦,因为它听起来像一个基本功能...有什么想法吗?
编辑 1:
像这样,根据 post 类型
显示不同数量的 post
add_action( 'pre_get_posts', function ( $query ) {
if( ! is_admin() && $query->is_search() && $query->is_main_query() && is_post_type == 'MY_POST_TYPE_1' ) {
$query->set( 'posts_per_page', 1 );
} elseif ( ! is_admin() && $query->is_search() && $query->is_main_query() && is_post_type == 'MY_POST_TYPE_2' ) {
$query->set( 'posts_per_page', 10 );
};
} );
要根据查询 CPT 添加额外的条件查询参数,我们可以像您一样使用 pre_get_posts
挂钩来完成此操作。
检查查询是否针对 CPT
我们将创建一个自定义函数来挂接到此操作并检查 $query
是否包含 CPT,如果包含则添加额外的参数。正如您也想到的那样,这将起作用:
add_action( 'pre_get_posts', 'add_cpt_query_args');
function add_cpt_query_args( $query ) {
// If this isn't the main search query on the site, then do nothing.
if( is_admin() || !$query->is_search() || !$query->is_main_query() )
return;
// Check if the query is for any of our CPTs
if (in_array ( $query->get( post_type'), array('YOUR_CPT_SLUG', 'ANOTHER_CPT') ) ){
// set the args for this CPT
$query->set( 'posts_per_page', 1 );
$query->set( 'orderby', 'rand' );
}
}
检查多个 CPT 和不同的查询参数
由于您想为多个 CPT 执行此操作并为它们提供不同的参数(例如 posts_per_page
),因此您必须在上面的代码中为每个 CPT 添加一个单独的 if
。
一种更灵活且更易于维护的方法 是使用数组将每个 CPT 的所有查询参数信息保存在一个数组中。这意味着:
- 我们可以简单地遍历它以使用添加我们的查询参数 - 无需为每个要检查的 CPT 添加额外的
if
检查
- 我们可以根据需要为每个 CPT 添加尽可能多或尽可能少的查询参数(例如
pages_per_post
、order_by
等),每个 CPT 可以有不同的参数,并且不必将它们硬编码到代码中,或者手动检查我们是否有它们的值。
我们可以用这段代码做到这一点——下面有它如何工作的完整解释
(注意 - 这是未经测试的,但基本逻辑就在那里!):
add_action( 'pre_get_posts', 'add_cpt_query_args');
function add_cpt_query_args( $query ) {
// If this isn't the main search query on the site, then do nothing.
if( is_admin() || !$query->is_search() || !$query->is_main_query() )
return;
// As you want to do this for multiple CPTS, a handy way to do this is set up all the info in an array
$CPT_args['qcm'] = array('posts_per_page'=> 1, 'orderby' => 'rand');
$CPT_args['anotherCPT'] = array('posts_per_page'=> 5, 'orderby' => 'date', 'order => 'ASC'');
// Now check each CPT in our array to see if this query is for it
foreach ($CPT_args as $cpt => $query_args ){
if (in_array ( $query->get( post_type'), array($cpt) ) ){
// this query is for a CPT in our array, so add our query args to the query
foreach ($query_args as $arg => $value )
$query->set( $arg, $value );
}
}
}
这是如何运作的
1.检查这是否是主站点上的主要搜索查询 - 如果不是(并且我们没有其他条件),最干净的方法是在此时简单地结束该功能,那么我们就不是试图在 if
中保留大量代码
if( is_admin() || !$query->is_search() || !$query->is_main_query() )
return;
2。在数组中设置您的 CPT 参数 - 因为您想为多个 CPT 执行此操作,最简单的方法是将所有信息存储在一个数组中,我们可以在循环中简单地使用它们。
- 我们将在这个例子中使用一个名为
$CPT_args
的数组,并为我们要检查的每个 CPT 添加一个条目,以 CPT 名称作为键。
- 我们将使用另一个数组作为值,这将包含您要添加到此 CPT 的
$query
的所有参数,例如
$CPT_args['qcm'] = array('posts_per_page'=> 1, 'orderby' => 'rand');
使用此数组将所有参数信息保存在一个地方,并且可以轻松添加或删除更多 CPT 条件,而不会影响影响查询的主要代码。
3。检查 $query
是否适用于 CPTS 中的任何一个,方法是循环遍历我们的数组并根据 $CPT_args
数组中的键检查查询 post_type
:
foreach ($CPT_args as $cpt => $query_args ){
if (in_array ( $query->get( post_type'), array($cpt) ) ){
...
}
}
4。如果此查询是针对该 CPT,则将 CPT 的查询参数添加到 $query
。我们可以简单地查看我们的 args 数组并将它们添加到 $query
.
foreach ($queryargs as $arg => $value )
$query->set( $arg, $value );
我正在尝试根据 post 类型在搜索页面上实现 post 数量(post 计数)的条件显示。
我正在寻找这样的东西:
add_action( 'pre_get_posts', function ( $query ) {
if( ! is_admin() && $query->is_search() && $query->is_main_query() && is_post_type == 'MY_POST_TYPE' ) {
$query->set( 'posts_per_page', 1 );
};
} );
我可以在哪里使用 is_post_type == 'MY_POST_TYPE'
来指定 post 类型...我一直在四处寻找但无法找到答案...这有点麻烦,因为它听起来像一个基本功能...有什么想法吗?
编辑 1: 像这样,根据 post 类型
显示不同数量的 postadd_action( 'pre_get_posts', function ( $query ) {
if( ! is_admin() && $query->is_search() && $query->is_main_query() && is_post_type == 'MY_POST_TYPE_1' ) {
$query->set( 'posts_per_page', 1 );
} elseif ( ! is_admin() && $query->is_search() && $query->is_main_query() && is_post_type == 'MY_POST_TYPE_2' ) {
$query->set( 'posts_per_page', 10 );
};
} );
要根据查询 CPT 添加额外的条件查询参数,我们可以像您一样使用 pre_get_posts
挂钩来完成此操作。
检查查询是否针对 CPT
我们将创建一个自定义函数来挂接到此操作并检查 $query
是否包含 CPT,如果包含则添加额外的参数。正如您也想到的那样,这将起作用:
add_action( 'pre_get_posts', 'add_cpt_query_args');
function add_cpt_query_args( $query ) {
// If this isn't the main search query on the site, then do nothing.
if( is_admin() || !$query->is_search() || !$query->is_main_query() )
return;
// Check if the query is for any of our CPTs
if (in_array ( $query->get( post_type'), array('YOUR_CPT_SLUG', 'ANOTHER_CPT') ) ){
// set the args for this CPT
$query->set( 'posts_per_page', 1 );
$query->set( 'orderby', 'rand' );
}
}
检查多个 CPT 和不同的查询参数
由于您想为多个 CPT 执行此操作并为它们提供不同的参数(例如 posts_per_page
),因此您必须在上面的代码中为每个 CPT 添加一个单独的 if
。
一种更灵活且更易于维护的方法 是使用数组将每个 CPT 的所有查询参数信息保存在一个数组中。这意味着:
- 我们可以简单地遍历它以使用添加我们的查询参数 - 无需为每个要检查的 CPT 添加额外的
if
检查 - 我们可以根据需要为每个 CPT 添加尽可能多或尽可能少的查询参数(例如
pages_per_post
、order_by
等),每个 CPT 可以有不同的参数,并且不必将它们硬编码到代码中,或者手动检查我们是否有它们的值。
我们可以用这段代码做到这一点——下面有它如何工作的完整解释 (注意 - 这是未经测试的,但基本逻辑就在那里!):
add_action( 'pre_get_posts', 'add_cpt_query_args');
function add_cpt_query_args( $query ) {
// If this isn't the main search query on the site, then do nothing.
if( is_admin() || !$query->is_search() || !$query->is_main_query() )
return;
// As you want to do this for multiple CPTS, a handy way to do this is set up all the info in an array
$CPT_args['qcm'] = array('posts_per_page'=> 1, 'orderby' => 'rand');
$CPT_args['anotherCPT'] = array('posts_per_page'=> 5, 'orderby' => 'date', 'order => 'ASC'');
// Now check each CPT in our array to see if this query is for it
foreach ($CPT_args as $cpt => $query_args ){
if (in_array ( $query->get( post_type'), array($cpt) ) ){
// this query is for a CPT in our array, so add our query args to the query
foreach ($query_args as $arg => $value )
$query->set( $arg, $value );
}
}
}
这是如何运作的
1.检查这是否是主站点上的主要搜索查询 - 如果不是(并且我们没有其他条件),最干净的方法是在此时简单地结束该功能,那么我们就不是试图在 if
if( is_admin() || !$query->is_search() || !$query->is_main_query() )
return;
2。在数组中设置您的 CPT 参数 - 因为您想为多个 CPT 执行此操作,最简单的方法是将所有信息存储在一个数组中,我们可以在循环中简单地使用它们。
- 我们将在这个例子中使用一个名为
$CPT_args
的数组,并为我们要检查的每个 CPT 添加一个条目,以 CPT 名称作为键。 - 我们将使用另一个数组作为值,这将包含您要添加到此 CPT 的
$query
的所有参数,例如
$CPT_args['qcm'] = array('posts_per_page'=> 1, 'orderby' => 'rand');
使用此数组将所有参数信息保存在一个地方,并且可以轻松添加或删除更多 CPT 条件,而不会影响影响查询的主要代码。
3。检查 $query
是否适用于 CPTS 中的任何一个,方法是循环遍历我们的数组并根据 $CPT_args
数组中的键检查查询 post_type
:
foreach ($CPT_args as $cpt => $query_args ){
if (in_array ( $query->get( post_type'), array($cpt) ) ){
...
}
}
4。如果此查询是针对该 CPT,则将 CPT 的查询参数添加到 $query
。我们可以简单地查看我们的 args 数组并将它们添加到 $query
.
foreach ($queryargs as $arg => $value )
$query->set( $arg, $value );