0

I want to print posts if Custom Field Value (Expiry) formatted as 31 April 2021 is equal to or greater than today date (19 April 2021). I have tried with the below code but not working.

    <table>
   <thead>
    <tr>
      <th>Title</th><th>Click</th>
    </tr>
   </thead>
 <?php
     $today = strtotime(date('d F Y'));
        $datecf = get_post_meta($post->ID, 'Expiry', true );
        $cfdate = (!empty($datecf))? strtotime($datecf) : false;
        if($today <= $cfdate){
    ?>
     <tbody>
        <tr>
          <td><?php the_title(); ?></td>
          <td><a href="<?php the_permalink(); ?>">Check Here</a> </span> </td> </tr>
     <?php
     } 
    // Posts not found
      else {
       ?>
       <tr> 
         <td><p>There is no Active Posts Now, Check Tommorrow!</p></td>
       </tr>
     <?php
       }
        ?> 
         </tbody>
           </table>

1 Answer 1

1

In this example , suppose expiry date is (19 April 2021) AND also your Today Date is equal like(19 April 2021) or greater then Like Tomorrow date (20 April 2021),So You will get all the posts from today's date, Here, date format is (dd-mm-yyyy) as defualt (19-04-2021). But, as your requirement format ('d F Y') like this (19 April 2021). So, as per my example you can use this format Like $today = strtotime(date('d F Y')); But must remember that, Your posts Custon field return value As per your requirement date format.

<table>
    <thead>
        <tr>
            <th>Title</th>
            <th>Click</th>
        </tr>
    </thead>
    <?php
//  $today   = strtotime(date('d F Y'));
    $today   = date('Y-m-d');
    $args    = array(
        'post_type'  => 'post',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key'     => 'expire_date',
                'value'   => $today,
                'compare' => '<=',
                'type'    => 'DATE'
            )
        )
    );
    $query   = new WP_Query($args);
    $ex_date = get_post_meta($post->ID, 'expire_date', true);
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <tbody>
                <tr>
                    <td><?php the_title(); ?></td>
                    <td><a href="<?php the_permalink(); ?>">Check Here</a></td> 
                </tr>
                <?php
            }
            ?>
            <?php
        } else {
            ?>
            <tr> 
                <td><p>There is no Active Posts Now, Check Tommorrow!</p></td>
            </tr>
            <?php
        }
        ?> 
    </tbody>
</table>

2
  • Thanks @vishwa This works, now after implementing this on the taxonomy custom template then it returns only one post value, but that taxonomy has 3 posts that satisfy date condition but are not printed by the code, I tried posts_per_page' => -1, but still not works. Apr 22, 2021 at 17:47
  • hello @PuneetVerma, modify this line into your code and try it. 'compare' => '<=' also include posts_per_page' => -1 you will get all posts equal or greater than today Date
    – vishwa
    May 3, 2021 at 5:32

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.