普通网友 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 win11安卓子系统打开谷歌登录不了切使用浏览器显示处于离线状态
    • ¥15 三维直角坐标系下,给定一个平面内四个点坐标,如何将四个点逆时针排序
    • ¥15 UnityWebRequest访问内网地址502
    • ¥20 Python语言来实现牛顿法(Newton's Method)解决非线性(系统)方程的问题。
    • ¥15 matlab控制工业相机采集图像
    • ¥25 R里做折线图和柱状图
    • ¥20 使用kokkos移植项目,遇到问题
    • ¥15 求该问题的Matlab代码
    • ¥15 python下使用pdpbox为何提示has no attribute 'pdp_isolate'?
    • ¥15 求java web病房管理系统项目,用netbeans做的