组合排序和随机CPT

时间:2021-01-13 作者:zuperuser

我有两种自定义帖子类型,希望在同一个查询中显示其中一种按标题排序,另一种在随机位置显示。

So CPT A(排序)en CPT B(随机):

A1 A2 A3B2 A4 A5B5 A6 A7 A8 A9B1 ...
或A1B1 A2 A3B5 A4 A5 A6 A7 A8 A9B7 ...

B4 A1 A2 A3 A4B3 A5 A6 A7 A8B1 A9。。。

我尝试合并两个CPT,但没有按预期效果。

$projectposts = get_posts(array(
 \'posts_per_page\' => -1,
 \'post_type\'      => \'project\',
 \'order\'          => \'ASC\'
));
        
//second query
$storieposts = get_posts(array(
 \'posts_per_page\' => -1,
 \'post_type\'      => \'storie\',
 \'order\'          => \'rand\'
));
        
$mergedposts = array_merge( $projectposts, $storieposts ); //combined querie
@kero:
我有这个↓ 它几乎起作用了。。。看起来这会在数组中创建一些空条目。(我百分之百确定在合并之前没有空标题。)
查询结果为A3 A2 A4 A6 B2(空)(空)(空)A1(空)(空)B1 B3。。。

   $projectposts = get_posts([
     \'posts_per_page\' => -1,
     \'post_type\'      => \'project\',
     \'order\'          => \'ASC\',
     \'meta_query\'     => array(
        array(
          \'key\'     => \'featured\',
          \'value\'   => \'"homepagina"\',
          \'compare\' => \'LIKE\'
        )
      )
   ]);
   
   $storieposts = get_posts([
       \'posts_per_page\' => -1,
       \'post_type\'      => \'story\',
       \'order\'          => \'ASC\',// not rand!
       \'meta_query\'     => array(
          array(
            \'key\'     => \'featured\',
            \'value\'   => \'"homepagina"\',
            \'compare\' => \'LIKE\'
          )
        )
   ]);
   
   function array_random_merge(array $a, array $b): array {
       while (count($b) > 0) {
           shuffle($b);
           // get one entry of $b
           $item = array_shift($b);
           // find random position inside $a
           $pos = mt_rand(0, count($a) - 1);
           // insert $item into $a
           array_splice($a, $pos, 0, $item);
       }
       return $a;
   }
   
   $mergedposts = array_random_merge($projectposts, $storieposts);
   
    if( $mergedposts ) {
      foreach( $mergedposts as $post ) { 
        
        echo \'<h3>\';
        echo the_title();
        echo \'</h3>\';
        
      }
    }
    wp_reset_postdata(); 
    

2 个回复
SO网友:burlakvo

尝试获取两个不同的post数组,并在数组A中循环。在获取元素的post数据之前,从数组B中获取随机元素。

例如:

$array_a = [\'post_id_a1\', \'post_id_a2\', \'post_id_a3\'];
$array_b = [\'post_id_b1\', \'post_id_b2\', \'post_id_b3\'];

foreach ( $array_a as $a_post_id ) {
    $is_b_should_displayed = mt_rand( 0, 1 );

    if ( $is_b_should_displayed && $b_length = count( $array_b ) ){
        $b_to_show = mt_rand( 0, $b_length - 1 );

        // show random B post

        unset( $array_b[ $b_to_show ] );
        $array_b = array_values( $array_b ); // to reorder array after unset
    }

    // show A post
}
或者以同样的方式创建数组C,然后通过C循环显示帖子。没有保证显示所有B,因此,如果需要,可以在主A循环后通过B循环(如果至少存在1个元素)

小注释:rand 用于键orderby 不要根据wp docs.

希望有帮助

SO网友:kero

我可能会这样做:

$projects = get_posts([
    \'posts_per_page\' => -1,
    \'post_type\'      => \'project\',
    \'order\'          => \'ASC\',
]);

$stories = get_posts([
    \'posts_per_page\' => -1,
    \'post_type\'      => \'storie\',
    \'order\'          => \'ASC\',// not rand!
]);

function array_random_merge(array $a, array $b): array {
    while (count($b) > 0) {
        shuffle($b);
        // get one entry of $b
        $item = array_shift($b);

        // find random position inside $a
        $pos = mt_rand(0, count($a) - 1);
        // insert $item into $a
        array_splice($a, $pos, 0, $item);
    }
    return $a;
}

$merged = array_random_merge($projects, $stories);
这里的主要思想如下:

移除一个$item 从…起$b$a$b 为空Per Tom\'s comment 我删除了rand 查询中的顺序和简单shuffle() 阵列。