我有一个工作查询,它返回一组按包 ID (ASC) 排序的自定义帖子。 对于具有 9 个结果的查询,这可能返回例如:
4 个帖子(帖子 ID 为 1、2、3、4),包 ID 为 1
包 ID 为 2 的 3 个帖子(帖子 ID 的 5、6、7)
2 个帖子(帖子 ID 为 8,9),包 ID 为 3
当这些帖子显示时,它们总是以相同的顺序排列。 换句话说,它们按照从 Post ID 1 到 Post ID 9 的顺序显示。
我想要实现的是随机对每个子集(由包 ID 定义)的结果进行排序。 这样,帖子将显示为:
随机显示帖子 ID 的 1 到 4(例如 2、1、4、3)
随机显示帖子 ID 的 5 到 7(例如 6、5、7)
随机显示帖子 ID 的 8 到 9(例如 8,9)
所以帖子仍然按包 ID 分组,但每个包中的帖子随机显示。
这是我当前查询的样子:
$args = array(
'post_type' => 'custom_post_type',
'post_status' => 'publish',
'posts_per_page' => 9,
'meta_key' => 'pkg_id',
'orderby' => 'pkg_id',
'order' => 'ASC'
);
我认为“可能”起作用但不起作用的方法:
$args = array(
'post_type' => 'custom_post_type',
'post_status' => 'publish',
'posts_per_page' => 9,
'meta_key' => 'pkg_id',
'orderby' => array( 'pkg_id' => 'ASC', 'rand' )
);
任何建议,因为我完全被难住了!
无需执行过多的查询即可完成此操作。 你仍然可以使用 仅有的 一个查询, the_posts
过滤器和一些 PHP 来根据需要对代码进行排序。
我认为这是我在您的问题中阅读的自定义查询,因此我们可以执行以下操作:
-
首先,我们要介绍一个习俗
WP_Query
参数,以便我们可以将该参数用作我们的触发器the_posts
筛选。 我们称这个参数为 nwpse_custom_sort
它的价值是true
为了触发过滤器 -
接下来,我们将使用
the_posts
过滤以根据自定义字段对帖子进行排序,然后根据自定义字段对这些帖子进行随机排序,最后返回排序后的帖子
代码
虽然只是一些注意事项
-
该代码未经测试,至少需要 PHP 5.4
-
我用过
get_post_meta()
要返回每个帖子的自定义字段值,因为您正在使用 ACF,您可能需要调整它以使用get_field()
. 我不熟悉 ACF,所以你需要对那部分进行排序。 虽然逻辑仍然保持不变 -
请记住,查询自定义字段不会导致额外的查询,因为它们被缓存了,所以这是一种非常精简、优化的方式来实现您的最终目标
这是自定义查询的代码。 您基本上需要做的就是将额外参数添加到自定义页面模板中的查询参数中
// Run your query normally
$args = [
'wpse_custom_sort' => true, // This parameter will trigger or the_posts filter
// Your query args here
];
$q = new WP_Query( $args );
// Your loop as normal
现在,我们将运行我们的过滤器(这进入 functions.php
或者最好是自定义插件)
/**
* Function to flatten a multidimentional array (for later use)
*
* Credit to zdenko
* @link https://gist.github.com/kohnmd/11197713
*/
function flatten_array( $arg )
{
return is_array( $arg )
?
array_reduce( $arg, function ( $c, $a )
{
return array_merge( $c, flatten_array( $a ) );
}, [] )
:
[$arg];
}
// The the_posts filter
add_filter( 'the_posts', function ( $posts, $q )
{
// First we need remove our filter
remove_filter( current_filter(), __FUNCTION__ );
// Check if our custom parameter is set and is true, if not, bail early
if ( true !== $q->get( 'wpse_custom_sort' ) )
return $posts;
// Before we do any work, and to avoid WSOD, make sure that the flatten_array function exists
if ( !function_exists( 'flatten_array' ) )
return $posts;
// Our custom parameter is available and true, lets continue
// Loop through the posts
$major_array = [];
foreach ( $posts as $p ) {
$meta = get_post_meta(
$p->ID,
'pkg_id',
true
);
// Bail if we do not have a $meta value, just to be safe
if ( !$meta )
continue;
// Sanitize the value
$meta = filter_var( $meta, FILTER_SANITIZE_STRING );
// Lets build our array
$major_array[$meta][] = $p;
}
// Make sure we have a value for $major_array, if not, bail
if ( !$major_array )
return $posts;
// Lets randomize the posts under each custom field
$sorted = [];
foreach ( $major_array as $field )
$sorted[] = shuffle( $field );
// Lets flatten and return the array of posts
$posts = flatten_array( $sorted );
return array_values( $posts );
}, 10, 2 );
如果我们使用:
$args = array(
'post_type' => 'custom_post_type',
'post_status' => 'publish',
'posts_per_page' => 9,
'meta_key' => 'pkg_id',
'orderby' => array( 'pkg_id' => 'ASC', 'rand' => 'DESC' )
);
然后我们得到订单部分为:
ORDER BY wp_postmeta.meta_value ASC, RAND() DESC
这似乎可行,但不需要 DESC。
但请注意,订购 RAND()
不能很好地扩展。 在这种情况下,@PieterGoosen 的方法可能更适合您。