I have a situation where I have a query that needs to display a list of “previous sales”. That’s easy enough. BUT I also need to put any of the sales that have a “Priority Number” (a custom number field) first in the list, organized by their priority number. On our homepage we’re showing 4 items in the list. So if there are 4 items with a priority number my system works. The issue is if I have less then 4 items with priority. In that case I want to show a list of sales that is organized by “sale end date” in descending order (to make a total of 4 items in the list). Here is the code I’m using now, something breaks down if there are no assets with priority numbers, the list I get is not correct, it’s close, but it’s not pulling the correct previous sales, it’s skipping some of them. Can you see anything that’s obviously wrong with my code? Here is the site it’s residing on, it’s the third column that’s the problem. http://www.newmillcapital.com
<!
Start Column Three
>
<div class="hp-col3">
<div class="BtnContainer">
<a href="previous-sales" class="Btn">Previous Sales</a>
</div>
<div class="highlights outer-wrapper" style="margin-bottom: 30px;">
</div>
<div class="content">
<?php
$todaysdate = date("Ymd"); // Get current unix timestamp
$count = 0; //set up counter variable
$args = array (
'posts_per_page' => 4,
'post_type' => 'asset_sales', // your event post type slug
'post_status' => 'publish', // only show published events
'meta_key' => 'asset_homepage_priority', // your priority field
'orderby' => 'meta_value', // order by value of asset_homepage_priority field
'order' => 'DESC', // order in DESC order
'meta_value' => 0,
'meta_compare' => '>',
'meta_query' => array(
array(
'key' => 'sale_end_date',
'value' => $todaysdate,
'compare' => '<',
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post(); // Start loop
$prioritynumber = get_field('asset_homepage_priority');
$saleenddate = get_field('sale_end_date', false, false);
$saleenddate = new DateTime($saleenddate);
$count++; //increment the variable by 1 each time the loop executes
?>
<?php if ($count < 5) { ?>
<ul class="block posts-list thumb">
<div class="highlights">
<article style="">
<div class="cf listing-meta meta above" style="">
<time class="meta-item" style="">Ended: <?php //the_field('asset_homepage_caption');?><?php echo $saleenddate->format('F j, Y'); echo " " . $prioritynumber; ?></time></div>
<a href="<?php the_permalink() ?>" class="image-link">
<?php the_post_thumbnail('overlay-large'); ?>
</a>
<div style="width: 100%; height: 110px;"> </div>
<h2 class="post-title" style=""><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="excerpt">
<?php echo Bunyad::posts()->excerpt(null, Bunyad::options()->excerpt_length_highlights, array('add_more' => false)); ?>
</div>
</article>
</div>
</ul>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php
$args = array (
'posts_per_page' => 4,
'post_type' => 'asset_sales', // your event post type slug
'post_status' => 'publish', // only show published events
'meta_key' => 'asset_homepage_priority', // your priority field
'order' => 'DESC', // order in DESC order
'meta_value' => '',
'meta_compare' => '=',
'meta_query' => array(
array(
'key' => 'sale_end_date',
'value' => $todaysdate,
'compare' => '<',
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post(); // Start loop
$prioritynumber = get_field('asset_homepage_priority');
$saleenddate = get_field('sale_end_date', false, false);
$saleenddate = new DateTime($saleenddate);
$count++; //increment the variable by 1 each time the loop executes
?>
<?php if ($count < 5) { ?>
<ul class="block posts-list thumb">
<div class="highlights">
<article style="">
<div class="cf listing-meta meta above" style="">
<time class="meta-item" style="">Ended: <?php //the_field('asset_homepage_caption');?><?php echo $saleenddate->format('F j, Y'); echo " " . $prioritynumber; ?></time></div>
<a href="<?php the_permalink() ?>" class="image-link">
<?php the_post_thumbnail('overlay-large'); ?>
</a>
<div style="width: 100%; height: 110px;"> </div>
<h2 class="post-title" style=""><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="excerpt">
<?php echo Bunyad::posts()->excerpt(null, Bunyad::options()->excerpt_length_highlights, array('add_more' => false)); ?>
</div>
</article>
</div>
</ul>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<a href="previous-sales" class="ViewAllBtn"><div class="ViewAllBtnContainer">View All Previous Sales</div></a>
<div style="clear: both;"></div>
</div>
<!
END Column THREE
>