【问题标题】:Wordpress : Custom order for custom post types on WP_QueryWordpress:WP_Query 上自定义帖子类型的自定义顺序
【发布时间】:2015-08-31 12:40:30
【问题描述】:

我在 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 );

我想知道有没有办法像这个订单一样导出结果

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

由于它们是不同的帖子类型,我不能使用正常的 ASCDESC 订单。

还有其他方法吗?

【问题讨论】:

标签: php wordpress


【解决方案1】:

嗯,我用这种方法来解决问题。 如果以后有人需要,想分享:

$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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2014-10-13
    • 2017-09-08
    • 2012-01-11
    • 2015-02-16
    相关资源
    最近更新 更多