I´m using custom post types on my site and i need to sort these coupons in this way:
- New Coupon ending TODAY ---
- New Coupon ending 05.03.2015
- New Coupon ending 06.03.2015
- New Coupon ending 07.03.2015
- ---
- Old Coupon expired 03.03.15
- Old Coupon expired 02.03.15
- Old Coupon expired 01.03.15
These coupons have a custom field named unixendet with unix values (tells when a coupon is ending) in it - F.x.: unixendet = 1428271200
Now i´m using this code for querying two lists in one:
<?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:
I have 48 coupons: 11 valid ones from args1 AND 37 expired ones from args2
- first page: 10 coupons from args1 AND 10 from args2
- second page: 1 coupon from args1 AND 10 from args2
- third page: 0 coupons from args1 AND 10 from args2
- Problem 1: On the second page there is 1 valid coupon, so it would make no sense to sort like this. I want all valid ones stay above the expired ones.
- Problem 2: It seems that 7 coupons are missing from args2 (page number 4 doesn´t exist.) So only 30 from 37 expired posts are showing up.
Hope somebody can help.
Aucun commentaire:
Enregistrer un commentaire