cwh*_*iro 3 php sorting wordpress
我想通过合并 2 个单独get_posts查询的结果来创建单个帖子数组,并让该数组按发布日期排序。
$args_b在我下面的代码中,和 的get_posts$args_a已合并到一个数组中,但它们是分开的:$args_b首先列出 的 9 个标题,然后列出 的 9 个标题$args_a。我希望它们混合在一起,并按日期排序。我如何对它们进行排序?
<?php
    $args_a = array(
    'posts_per_page' => 9,
    'category_name' => 'shinmatsudo',
    'category__in' => array( 227 ),
    'category__not_in' => array( 3 ),
    'meta_query' => array(
                          'relation' => 'AND',
                                              array(
                                                    'key'     => '1b',
                                                    'compare' => 'NOT EXISTS'
                                                     ),
    
                                               array(
                                                    'key'     => '1d',
                                                    'compare' => 'NOT EXISTS'
                                                     ),
                                                                
                          ), 
    );?>
    
    
    <?php
    $args_b = array(
    'category_name' => 'matsudo',
    'posts_per_page' => 9,
    'category__in' => array( 329 ),
    'category__not_in' => array( 3 ),
    'meta_query' => array(
                    'relation' => 'and',
                                    array(
                                    'key'=> '2a',
                                    'value' => array('2020-02-01' , '2020-06-01'),
                                    'compare' => 'BETWEEN',
                                    'type' => 'DATE',
                                    ),
                        ),
    );
    ?>
    
    
    <?php
    global $post;
    $my_posts= array_merge( get_posts($args_b), get_posts($args_a) );
    
    
    foreach($my_posts as $post):setup_postdata($post);?>
    <?php the_title(); ?>
        
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
Run Code Online (Sandbox Code Playgroud)
    合并两个单独查询的结果不会改变顺序,因此您需要自己对它们进行排序。
因为get_posts返回一个对象数组,所以我们需要使用一个排序函数来创建用户定义的比较(例如usort) - 换句话说,我们告诉它要对哪些值进行排序以及以什么顺序排序。
如果您使用PHP 7,您可以使用匿名函数进行排序和“太空船运算符”<=>进行比较,所以这就是您所需要的:
usort($my_posts, function($post_a, $post_b) {
    // Compare the post_date of the posts, and if $post_b is newer, then it will be displayed first
    return $post_b->post_date <=> $post_a->post_date;
});
Run Code Online (Sandbox Code Playgroud)
(请注意,通常您会按return $a->value <=> $b->value;升序返回结果,但由于我们正在处理日期并希望首先获得最新的结果,因此我们需要将比较反转为return $b->value <=> $a->value;)
所以在你的代码中,你只需要像这样使用它:
<?php
// do your merge...
$my_posts= array_merge( get_posts($args_b), get_posts($args_a) );
// sort the resulting array...
usort($my_posts, function($post_a, $post_b) {
    return $post_b->post_date <=> $post_a->post_date;
});   
// and now process the new sorted array as required.    
foreach($my_posts as $post):setup_postdata($post);?>
    <?php the_title(); ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
Run Code Online (Sandbox Code Playgroud)
更新:如果您仍在使用PHP 5(您不应该这样!),您可以使用以下命令:
usort($my_posts, function ($a, $b) {
    if ($a->post_date < $b->post_date)       return -1;
    elseif ($a->post_date > $b->post_date)   return 1;
    else                                     return 0;
});
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           1662 次  |  
        
|   最近记录:  |