I have a custom post type called client and it has a custom field of next_due_date, that is a date without time. I want to have a list of posts that have a next due date that is coming up soon. I tried creating a page template called "due soon" with this code (including changes suggested by Tim Malone):

$loop = new WP_Query(array(
    'post_type'      => 'client',
    'posts_per_page' => -1,
    'orderby'        => 'meta_value_num',
    'order'          => 'ASC',
    'meta_key'       => 'next_due_date',
    'meta_query'     => array(array(
        'key'     => 'meta_value_num',
        'value'   => date('YY-mm-dd',strtotime("today")+(7*60*60*24)),
        'compare' => '<=',
        'type'    => 'DATE'
while ( $loop->have_posts() ) : $loop->the_post();
    responsive_entry_before(); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <?php responsive_entry_top(); ?> 
        <h2 class="entry-title post-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>  
        <div class="post-entry">
            <?php echo "File # : " . get_post_meta($post->ID,'file_number',true) . "<br />"; ?>
            <?php echo "File Status : " . get_post_meta($post->ID,'file_status', true) . "<br />"; ?>
        </div><!-- end of .post-entry -->
        <div class="post-meta">
            <?php responsive_post_meta_data(); ?>
            <?php if ( comments_open() ) : ?>
                <span class="comments-link">
                <span class="mdash">&mdash;</span>
                <?php comments_popup_link( 
                    __( 'No Comments &darr;', 'responsive' ), 
                    __( '1 Comment &darr;', 'responsive' ), 
                    __( '% Comments &darr;', 'responsive' ) ); ?>
            <?php endif; ?>
        </div><!-- end of .post-meta -->

        <?php responsive_entry_bottom(); ?>
    </div><!-- end of #post-<?php the_ID(); ?> -->
    <?php responsive_entry_after();

I haven't been able to get it to output anything, let alone figured out how to define coming soon (say 7 days).

Here's the code I'm using to count the post types and ones with a field set to open:

global $wpdb;
$meta_key = 'file_status';
$meta_value = 'open';

$sql = "SELECT count(DISTINCT pm.post_id)
FROM $wpdb->postmeta pm
JOIN $wpdb->posts p ON (p.ID = pm.post_id)
WHERE pm.meta_key = '$meta_key'
AND pm.meta_value = '$meta_value'
AND p.post_type = 'client'
AND p.post_status = 'publish'
$count = $wpdb->get_var($sql);
$count_posts = wp_count_posts('client');
$published_posts = $count_posts->publish;
echo "Number of Clients: " . $published_posts;
echo "<p>Open Files: $count</p>";

I'd love to also count the ones with due dates coming up


Firstly, the_post() doesn't actually output anything - it just sets up the post data ready to output. You'll need to do something like this to show the posts:

while ( $loop->have_posts() ) :

There's several more tags you can use and you'll want to format the HTML around them to display how you want to, let me know if you need guidance on that too.

As for the query, you'll probably find this very useful: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

You're pretty close with the ordering, but to limit your posts returned to anything within the next 7 days you'll need to use a meta_query. Here's how you do that:

$loop = new WP_Query(array(
    'post_type'      => 'client',
    'posts_per_page' => -1,
    'orderby'        => 'meta_value_num',
    'order'          => 'ASC',
    'meta_key'       => 'next_due_date',
    'meta_query'      => array(array(
            'relation'=> 'AND',
                 'key'     => 'next_due_date',
                 'value'   => date('Y-m-d',strtotime("today")+(7*60*60*24)),
                 'compare' => '<=',
                 'type'    => 'DATE'
                 'key'     => 'next_due_date',
                 'value'   => date('Y-m-d',strtotime("today")),
                 'compare' => '>=',
                 'type'    => 'DATE'                     

I haven't tested this code but I think it's correct - let me know how you go. The 7 days is defined by the 7*60*60*24 - 60 seconds x 60 minutes x 24 hours x 7 days.

UPDATED as per comments to also filter out posts in the past

  • That looks like it's correct. I tried it but the output was still blank. based on the date format in the DB Ichanged Y-m-d to YY-mm-dd, still nothing. heres the full code based on what you posted: Edit,it wouldn't let me put the code here so i put it into the next answer – mcmasterp Feb 24 '15 at 0:20
  • Sorry... I'm silly, I didn't use the right key. I've just changed the key in the meta_query to next_due_date, try it again now! – Tim Malone Feb 24 '15 at 0:39
  • Also, the format for date() is based on php.net/date - YY-mm-dd will not work because it will print out everything twice (eg. Y corresponds to the 4 character year, so YY will print out 20152015). If the database value is only two characters for the year and the above still isn't working, you might want to try lowercase y instead which will print out a two character year - eg. 15. However I'm fairly certain your database value will be four characters. – Tim Malone Feb 24 '15 at 0:43
  • yup that was it. it works perfectly now. thank you. Is there a way to count the number of posts that are within the date range? I am adding the code to my original question that I use to count the number of posts in my custom type and how many have a yes/no field set to open. I'd love to be able to also show a count of ones with due dates coming up – mcmasterp Feb 24 '15 at 4:07
  • Oh I just noticed that when the due date passes it still shows in the list. Is there a way to filter past due ones out? – mcmasterp Feb 24 '15 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.