普通网友 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 时间序列预测算法 预测结果出现负值
  • ¥15 苹果mac m1系统在关闭sip后怎么能使用app
  • ¥15 在win系统Django虚拟环境下载mysqlclient报错
  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼
  • ¥15 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计