简体   繁体   中英

Wordpress : Custom order for custom post types on 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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM