普通网友 2015-08-31 12:40
浏览 58
已采纳

Wordpress:WP_Query上自定义帖子类型的自定义顺序

I'm calling two different post types in WordPress:

$args = array( 'post_type' => array('post','testimonial'), 'posts_per_page' => 6 ,
                   'meta_key'   => 'show_on_home',
    'meta_value' => true
                 );
$loop = new WP_Query( $args );

I want to know if there is any way to export result like this order

"post-testimonial-post-testimonial-post-testimonial"

since they are different post types I can't use normal ASC or DESC orders.

Is there any other way?

  • 写回答

1条回答 默认 最新

  • duanqujing3863 2015-09-02 18:18
    关注

    Well, I used this method to solve problem. Wants to share if anyone need it later:

    $args = array('post_type' => array('post', 'testimonial'), 'posts_per_page' => 6,
                   'meta_key' => 'show_on_home',
                   'meta_value' => true,
                   'orderby' => 'type',
                   'exclude'=>1
                  );
                  $loop = new WP_Query($args);
    
                  $posts = $loop->get_posts();
    
    $a = $posts;
    $b = array(3, 0, 4, 1,5,2); // rule indicating new key order
    $c = array();
    foreach($b as $index) {
        $c[$index] = $a[$index];
    }
    $posts=$c;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条