I want to combine the 2 following queries so that ongoing promotions (query 1) shows before the rest of the promotions (query 2) but all together no more than 5 results show. Thanks.
Query #1 Ongoing Promotions
<?php
global $post;
$args = array(
'post_type' => 'promotions',
'numberposts' => 5,
'meta_key' => 'sp_ongoingPromotion',
'meta_value' => 1
);
$supplierCommunications = get_posts( $args );
foreach( $supplierCommunications as $post ) : setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>
<span>
<?php
echo "Ongoing Promotion";
?>
</span>
</li>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
Query #2 Current Promotions
<?php
global $post;
$args = array(
'post_type' => 'promotions',
'numberposts' => 5,
'meta_key' => 'sp_endDate',
'meta_value' => date("Y/m/d"),
'meta_compare' => '>=',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$supplierCommunications = get_posts( $args );
foreach( $supplierCommunications as $post ) : setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<br/>
<span>
<?php
$meta = get_post_meta(get_the_ID(), 'sp_endDate', true);
if('' != $meta) {
$formattedDate = new DateTime($meta);
echo "Expires: ".$formattedDate->format('F d, Y');
}
?>
</span>
</li>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
I wouldn’t combine the queries, primarily since they’re keying on different meta keys and values. The WP Query API is very powerful, but you’re trying to do some very advanced searching and sorting here.
One option would be to write a raw SQL query by hand and pass it into
$wpdb->get_results()
, but that won’t necessarily be future safe (i.e. if future versions change the DB schema).Instead, I recommend you do both queries separately and merge the two arrays.
Some (very rough) psuedocode: