Query using custom fields

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;">&nbsp;</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;">&nbsp;</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 ----------------->
  • Adam Czajczyk

    Hi Dave

    I hope you're fine today!

    I admit I still don't quite understand what exact output should that code produce. I mean, I do understand that the "priority" is the first thing to check and "order by" and if there's not enough records with priority set, other should be used. But I don't quite follow the desired logic. Could you perhaps explain it a bit more? Some imaginary example would be very helpful :slight_smile:

    Despite of that, I noticed two things in the code that I got some doubts about:

    1. The first query

    You're using this arguments

    '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' => '>',

    I assume that the goal here is to "order these 4 fetched posts by value of asset_homepage_priority field in descending order", right? Now, you got "meta_value => 0" and "meta_compare => >" there which seems fine but you also got an additional meta_query. Now, I'm wondering about that additional meta_query there that's supposed to also sort that by end date.. If you are only pulling there posts that have priority > 0 and sort them by priority, what is the point by sorting by date too? Or am I missing something?

    2. The date comparison

    Judging by the date format that your using in your code the data is not stored in the database as a Unix Timestamp but just as a string, like today for example would be:

    20180726

    That's fine but that won't be sorted properly because it's actually a string and with that sort of comparison in a sub-media_query:

    array(
    			'key'     => 'sale_end_date',
    			'value'   => $todaysdate,
    			'compare' => '<',
    		),

    would just sort it "alphabetically" and that might lead to "weird" order, which in turn might affect the rest of the query.

    With that date format, it might make more sense to sort that treating the date as a number so with arguments like this:

    array(
    			'key'     => 'sale_end_date',
    			'value'   => $todaysdate,
    			'compare' => '<',
                             'type' => 'NUMERIC'
    		),

    That's just couple of thoughts and I honestly admit that I'm not sure if they'll be helpful but I hope for some additional explanation on how exactly that should work and hopefully I'll then be able to help you a bit better :slight_smile:

    Best regards,
    Adam

  • Dave

    Hey Adam! Thank you so much for your well thought out reply. I need to get back in there and look at the code over the weekend, but let me see if I can explain the issue a little more thoroughly. You're right, the piece about the "sale end date" is important. I have a custom post type called "asset_sales" and it contains a bunch of property sales, some have happened, some haven't. So on our homepage column one list 4 sales that are upcoming. Column three list sales that have expired.
    But the client wants the ability to list some of the previous sales at the top of the column so we've given it the custom field of "asset_homepage_priority". The higher the number in this field, the closer to the top of the list. Then after the priority ones are listed we need the regular past sales (the past sale is determined if it is > than today's date). So the column would look like this:

    Previous Sale Item with priority number 999, descending order by priority number.
    Previous Sale Item with priority number 998, descending order by priority number.
    Regular past sale item, blank for priority number, descending order by date.
    Regular past sale item, blank for priority number, descending order by date.

    If there are four sales with priority numbers then all four would show. If there were no sales with priority numbers then only the regular past sale items would show. Easy right? I wish I was better at these queries, I'm a designer and I tend to cobble the PHP code together to get it to bend to my will. So I'm by no means an expert. Nor do I think I'm going about in the most elegant way. My dream would have one query that handles it all. No "count++", no second query to fill out the column. Just one concise bit of code. But that's a bit beyond me I'm afraid. Thank you so much for ANY direction you can lend.
    Best,
    Dave

  • Adam Czajczyk

    Hi Dave

    Thanks for response and additional explanation!

    I actually got some idea on how that might, perhaps, be achieved but I also have one more - quite fundamental - question:

    I'm guessing that the priority number is set by you somehow, manually, right? Are you 100% sure that only four posts will have the priority set at the time? I imagine that the case is easily possible where for example there are 6 posts in total with priority number set and e.g. two of them have priority of 3. What should happen then?

    Is it possible - in theory - that some posts instead of having priority not set at all, have it set to 0 instead?

    Let me know please.

    Best regards,
    Adam

  • Dave

    Hello again Adam! Thanks for working with me on this, your help is invaluable. Here's the answer to your questions.

    YES, the priority number is set manually for each item. And YES, it's very likely that there could be more than 4 items with priority numbers at a time. We're only displaying 4 (that could change down the road), and the list is by priority number in descending order.

    But NO, we can not set all of the others to zero, it will be a blank field. And their shouldn't be any duplicate priority numbers (like two having 999 or something) but if that does happen they could be listed one after the other. The purpose of the priority number is solely to show them in order on the homepage. It's a way for the client to make sure the most important sales are shown on the homepage.

    Does make sense?

    Thanks again my friend!
    Dave

  • Adam Czajczyk

    Hi Dave

    I gave it some thought and came up with slightly modified code for you to test. Just make sure that you do have a backup of your current one as I didn't exactly test it on my end yet...

    Here's the code and below it a word of explanation:

    <!----------------------- 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
    $excluded = array(); // we'll hold posts to exclude from next query here
    
    $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_query' => array(
    		array(
    			'key'     => 'sale_end_date',
    			'value'   => $todaysdate,
    			'compare' => '<',
    			'type' => 'numeric',
    		),
    	),
    );
    
    $query = new WP_Query( $args );
    
    $count = $query->post_count;
    
    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);
    array_push( $excluded, $post->ID );
    
    ?>
    <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;">&nbsp;</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 endwhile; ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    
    <?php
    ############## SECOND QUERY IF THERE's NOT ENOUGH PRIORITY POSTS #########################
    
    // proceed only if less than 4 posts were found above
    if ($count < 4 ) {
    
    	// set post limit based on how many post we got from previous query
    	$post_limit = 4 - $count;
    
    	$args = array (
    	'posts_per_page'         => $post_limit,
    	'post__not_in' 			=> $excluded,
    	'post_type'              => 'asset_sales', // your event post type slug
    	'post_status'            => 'publish', // only show published events
    	'meta_key'               => 'sale_end_date', // your priority field
    	'meta_type'				 => 'numeric',
    	'order'                  => 'DESC', // order in DESC order
    	'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;">&nbsp;</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>
    
    <?php 	
    
    }
    ?>
    
    <a href="previous-sales" class="ViewAllBtn"><div class="ViewAllBtnContainer">View All Previous Sales</div></a>
    
    <div style="clear: both;"></div>

    As you can see there are some slight differences in comparison to yours. The first one is in the first query arguments. It's basically the same but it doesn't even try to check whether the value of priority field is empty or bigger than 0 as it's not necessary - it will put the highest number on top anyway, then it will also sort that by the sale end date too. It also won't ever fetch more than 4 posts. So that part, is not much different (except for forcing the date comparison to be "numeric" due to the date structure).

    Then we got a loop that displays results. That's the same but I used a different way to count - the query should be returning the number of results anyway :slight_smile: Also, within the loop we're actually storing IDs of the posts that are displayed.

    Now the next step is to check whether there were 4 posts displayed or less. If for, the rest of the code is skipped because we don't need anything more. If less, the number of posts is calculated (which is "4 - $count") and that calculation result is how many posts we want to fetch with next query.

    There's also an "exclusion array" passed to query so to skip those posts that were already displayed. And then, the second query ignores the priority parameter at all and just takes remaining number of posts from the pool of absolutely all posts sorted by sale end date and that were not displayed yet.

    If I'm not mistaken, "logically" that should do the trick but like I said, I didn't have a chance to test the code. Give it a try and see if it does the job or, even if not, maybe it will give you some idea.

    In case you needed some further assistance, I'm afraid I would actually need some example set of data that you're using so some sort of export of the post types along with their custom fields and the data - to test it on.

    Best regards,
    Adam

  • Dave

    Adam, I couldn't wait. I had to see if your code worked. We're SO CLOSE. The priority sales list first in descending order by Priority Number. Perfect!

    The other previous sales list under that to fill out the column to 4. ALMOST perfect. The issue I'm seeing is that it's putting the non-priority ones in ASCENDING order, not descending (oldest sale end date first). The code has it set for DESC, so I know that's good (although I tried switching it to ASC just to see what would happen, no affect). Then I saw that 'orderby' => 'meta_value', was missing in the query so I tried inserting that. Again, no change. Any idea why it would be showing me the oldest first in ascending order?

  • Adam Czajczyk

    Hi Dave,

    Good to know that it's at least partially working :wink:

    You are of course right about missing "orderby" which should be there in the second query args. I reviewed the code again and noticed that I actually missed the "type" in a "sub meta query" there. I have also made yet another small change in code - in that second part - which I was about to make previously but, though that's a bit embarrassing, I forgot...

    If you look closer, you will notice that there's no more that additional "if ($count < 5)" check and the "wp_reset_postdata()" has been moved outside the loop (as it should be because inside the loop it can actually cause "strange behavior").

    Hopefully I didn't miss anything now so if you don't mind, let's give it another try with this updated code:

    <!----------------------- 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
    $excluded = array(); // we'll hold posts to exclude from next query here
    
    $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_query' => array(
    		array(
    			'key'     => 'sale_end_date',
    			'value'   => $todaysdate,
    			'compare' => '<',
    			'type' => 'numeric',
    		),
    	),
    );
    
    $query = new WP_Query( $args );
    
    $count = $query->post_count;
    
    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);
    array_push( $excluded, $post->ID );
    
    ?>
    <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;">&nbsp;</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 endwhile; ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    
    <?php
    ############## SECOND QUERY IF THERE's NOT ENOUGH PRIORITY POSTS #########################
    
    // proceed only if less than 4 posts were found above
    if ($count < 4 ) {
    
    	// set post limit based on how many post we got from previous query
    	$post_limit = 4 - $count;
    
    	$args = array (
    	'posts_per_page'         => $post_limit,
    	'post__not_in' 			=> $excluded,
    	'post_type'              => 'asset_sales', // your event post type slug
    	'post_status'            => 'publish', // only show published events
    	'meta_key'               => 'sale_end_date', // your priority field
    	'meta_type'				 => 'numeric',
    	'orderby'				=> 'meta_value',
    	'order'                  => 'DESC', // order in DESC order
    	'meta_query' => array(
    		array(
    			'key'     => 'sale_end_date',
    			'value'   => $todaysdate,
    			'compare' => '<',
    			'type' => 'numeric'
    		),
    	),
    );
    
    $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
    ?>
    
    <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;">&nbsp;</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 endwhile; ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    
    </div>
    
    <?php 	
    
    }
    ?>
    
    <a href="previous-sales" class="ViewAllBtn"><div class="ViewAllBtnContainer">View All Previous Sales</div></a>
    
    <div style="clear: both;"></div>

    Let's see how this goes then :slight_smile:

    Best regards,
    Adam

  • Dave

    Hi Adam! Well I tried this new code and I'm getting the exact same results. Priority numbers work, but the non-proiritized sales are not descending.

    In my test server I have two sales with Priority Numbers, and those always show first as they should. The other two sales that are showing up MIGHT be showing up because they're the oldest in the system. They have the lowest Page ID Numbers. That got me thinking. I changed the listing with the lowest Page ID so that the Sale End Date was yesterday (it was Aug. 2012) and it did NOT change the order on my homepage (it was still listing by lowest Page ID first).

    I then changed the Sale End Date to August 30, 2018 and the sale moved out of the Previous Sales and into the Current Sales (which is exactly as it should be). Then the Previous Sales column got the next sale Page Id to take it's place (still in ascending order). Does this give you any clues?

    I understand if you want to throw up your hands and give up.
    Dave

  • Adam Czajczyk

    Hi @dave159!

    Hm.... I'm sure we're missing something "small" here, probably even quite simple, but I admit I'm not sure what that is.

    I think we have reached a stage where I can't continue on a "theoretical level" and I'd need to work with some example data. Do you have some staging/dev site where I could "experiment" a bit more with that code?

    If yes, you could provide me with an access and I could give it another try. To provide me with access:

    Note: Don't leave your login details in this ticket.
    Instead, you can send us your details using our contact form https://premium.wpmudev.org/contact/#i-have-a-different-question and the template below:

    Subject: "Attn: Adam Czajczyk"
    - Site login URL
    - WordPress admin username
    - WordPress admin password
    - FTP credentials (host/username/password)
    - cPanel credentials (host/username/password)
    - Folder path to site in question
    - Link back to this thread for reference
    - Any other relevant urls/info

    As an alternative, I could just use some export of the example data but I'd also need to have a "structure" (custom post type and custom fields registered) so I'd need e.g. code that you are using for creating this or some detailed description of them.

    Best regards,
    Adam

  • Dave

    Ugh! Adam, you're gonna kill me but I have another wrinkle. As the client was testing this they discovered that if a Priority Number is given to a property that has a custom field checked for "saledate_TBD" is shows up in Previous Sales. The problem with these TBD sale dates is that they do not have a "sale end date" entered into the system. If you look at our test site you'll see that listing for "Major Construction Equipment Final Assembly Facility" is in column one and column three. Is there any way to exclude the TBD sales from column three? Sorry to keep asking.

  • Adam Czajczyk

    Hello @dave159!

    Haha, why should I kill you? I'm a peaceful and mellow creature. And we got a ban on killing Members since one of us kil... erm... just joking! I hope you don't mind, I just couldn't resist :wink:

    Anyway, we didn't discuss that "saledate_TBD" field so far, if I remember correctly, so I wasn't aware of it. But I think we can "think of something" :slight_smile: The questions are, though:

    1) the custom field name (meta name in db) is literally "saledate_TBD", right?
    2) what's the meta value of that field if the checkbox is checked and what is when it's not?
    3) I understand that for records with that field not checked, it's working fine so far, right?
    4) and if that field is checked
    - should such record be ignored all together (we're still speaking about that 3rd column we were working on, correct?)?
    - or if it has the "priority" set, it should be displayed in the "priority" part but if it doesn't have "priority" set it should be skipped?

    As for that 4th question, if it should behave in some yet another way, please explain. I'm sure we can come up with something then :slight_smile:

    Best regards,
    Adam

  • Dave

    Well good to know I'm not on the hit list (yet). Answers to your questions:
    1. yes, the field name in database is "saledate_TBD"
    2. This is actually a True/False field, not a checkbox (sorry, I misspoke)
    3. Yes, it works fine when it's not checked. The TBD is needed in the first column only.
    4. If the field is checked it should be ignored in the third column in all cases (checked, unchecked, with priority number and without). It should never appear in the third column.
    Basically the TBD means that the end date hasn't been set, so it's in the future and would NEVER be a Previous Sale.

  • Adam Czajczyk

    Hi @dave159!

    In that case we'll need to exclude all the records with that field set which actually means... including only those that got that field set to false :slight_smile: This could be done with an additional "meta_query. Try this please:

    In the first query arguments for the 3rd column, replace

    'meta_query' => array(
    		array(
    			'key'     => 'sale_end_date',
    			'value'   => $todaysdate,
    			'compare' => '<',
    			'type' => 'numeric',
    		),
    	),

    with

    'meta_query' => array(
    		'relation' => 'AND', // that's optional, it defaults to AND but I put it here for clarity
    		array(
    			'key'     => 'sale_end_date',
    			'value'   => $todaysdate,
    			'compare' => '<',
    			'type' => 'numeric',
    		),
    		array(
    			'key' => 'saledate_TBD',
    			'compare' => '!=',
    			'value' => '1',
    		),
    	),

    and in the 2nd query for 3rd column replace

    'meta_query' => array(
    		array(
    			'key'     => 'sale_end_date',
    			'value'   => $todaysdate,
    			'compare' => '<',
    			'type' => 'numeric'
    		),
    	),

    with

    'meta_query' => array(
    		'relation' = 'AND',
    		array(
    			'key'     => 'sale_end_date',
    			'value'   => $todaysdate,
    			'compare' => '<',
    			'type' => 'numeric'
    		),
    		array(
    			'key' => 'saledate_TBD',
    			'compare' => '!=',
    			'value' => '1',
    		),
    	),

    What is that doing, is basically adding an additional meta query that checks if "saledate_TBD" is different than "1" (so true, so is set) and sets relationship between these two meta queries to "AND" so both conditions must be met for the record to be displayed. In other words both the sale end date must be proper AND the saledate_TBD must not be true (checked) for the record to appear.

    Give it a go, please, and let me know if it worked for you.

    Best regards,
    Adam

  • Dave

    Hey Adam! Sorry it's taken me so long to get back to you (I got pulled into another project). But I was able to put your revised code in place and it worked! Brilliant!

    There was one small typo if anyone else is reading this thread and using the code.
    'relation' = 'AND', is missing the > character, should be 'relation' => 'AND',

    Thank you so much for all your hard work on this. I 'think' the client should be satisfied now (we'll see). Hopefully I won't have to bother you again (until the next time I get stuck of course!).

    All the best!
    Dave

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.