admin管理员组

文章数量:1287273

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>

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>
Share Improve this question asked Apr 18, 2021 at 17:51 PuneetPuneet 477 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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>

本文标签: Print Posts if Custom Field Value Date equal or greater than Today Date