我在我的网站上使用自定义帖子类型,我需要以这种方式对这些优惠券进行排序:
新优惠券于今日结束----2015年3月5日结束的新优惠券于2015年3月6日结束的新优惠券于2015年3月7日结束的新优惠券于2015年3月3日结束的旧优惠券于2015年3月3日到期的旧优惠券于2015年3月2日到期的旧优惠券于2015年3月1日到期的旧优惠券于2015年3月1日到期的旧优惠券在优惠券中有一个名为unixendet的自定义字段,其中包含unix值(告诉优惠券何时结束)-F.x.:unixendet森德特=1428271200
现在,我使用此代码在一个列表中查询两个列表:
<?php 
date_default_timezone_set(\'Europe/Berlin\'); setlocale(LC_TIME, "de_DE", "german");
$time = current_time( \'timestamp\' ); // Get current unix timestamp
$args = array(
    \'post_type\' => \'gutschein\',
    \'pagination\'             => true,
    \'posts_per_page\'         => \'10\',
    \'posts_per_archive_page\' => \'10\',
    \'ignore_sticky_posts\'    => false,
    \'post_status\' =>  \'published\',
    \'paged\' => $paged,
    \'meta_key\' => \'unixendet\',
    \'meta_value\' => $time, // Use the current time from above
    \'meta_compare\' => \'>=\', // Compare today\'s datetime with our event datetime
    \'orderby\' => \'meta_value_num\',
    \'order\' => ASC,
);
// get results
$first_query = new WP_Query( $args );
$args2 = array(
    \'post_type\' => \'gutschein\',
    \'pagination\'             => true,
    \'posts_per_page\'         => \'10\',
    \'posts_per_archive_page\' => \'10\',
    \'ignore_sticky_posts\'    => false,
    \'post_status\' =>  \'published\',
    \'paged\' => $paged,
    \'meta_key\' => \'unixendet\',
    \'meta_value\' => $time, // Use the current time from above
    \'meta_compare\' => \'<=\', // Compare today\'s datetime with our event datetime
    \'orderby\' => \'meta_value_num\',
    \'order\' => DESC,
);
$second_query = new WP_Query( $args2 );
$result = new WP_Query();
$result->posts = array_merge( $first_query->posts, $second_query->posts );
$result->post_count = count( $result->posts );
// The Loop
?>
<?php if( $result->have_posts() ): ?>
  <?php while ( $result->have_posts() ) : $result->the_post(); ?>
The big problem is how it outputs the posts:
我有48张优惠券:11张来自args1的有效优惠券和37张来自args2的过期优惠券
第一页:10张来自args1的优惠券和10张来自args2的优惠券第二页:1张来自args1的优惠券和10张来自args2的优惠券第三页:0张来自args1的优惠券和10张来自args2的优惠券问题1:在第二页上有1张有效优惠券,所以这样排序没有意义。我希望所有有效的保持在预期的之上问题2:args2中似乎缺少7张优惠券(第4页不存在)因此,37个过期帖子中只有30个出现希望有人能帮忙。
 
                    最合适的回答,由SO网友:birgire 整理而成
                    请注意,此部分:
    \'post_status\' =>  \'published\',
 应为:
    \'post_status\' =>  \'publish\',
 然后,您可以尝试以下操作:
$args = array(
    \'post_type\'              => \'gutschein\',
    \'pagination\'             => true,
    \'posts_per_page\'         => \'10\',
    \'posts_per_archive_page\' => \'10\',
    \'ignore_sticky_posts\'    => false,
    \'post_status\'            => \'publish\',
    \'paged\'                  => $paged,
    \'meta_key\'               => \'unixendet\',
);
add_filter( \'posts_orderby\', \'emti_orderby\' );
$q = new WP_Query( $args );
remove_filter( \'posts_orderby\', \'emti_orderby\' );
 我们在其中设置条件排序:
/**
 * Modify the order by part:
 */
function emti_orderby( $orderby )
{     
    global $wpdb;
    $time = current_time( \'timestamp\' ); // Get current unix timestamp
    return "
        CASE
            WHEN {$wpdb->postmeta}.meta_value >= {$time} 
                THEN  {$wpdb->postmeta}.meta_value+0 - {$time}+0      
            WHEN {$wpdb->postmeta}.meta_value < {$time} 
                THEN {$wpdb->postmeta}.meta_value+0
        END
        ASC
    ";
}