Rafael
Rafael

Reputation: 75

How do I delete a post through a meta_value wordpress

When I create a post in Wordpress, I determine an expiration date through a plugin called Post Expirator . My question is how I can delete these posts from the home page , categories and the like when they expire.

In addition , I would make all expired posts were displayed at a certain point of my site .

I have tried using the meta_keys and meta_value , but I'm not succeeding.

<?php $args = array(
            'meta_key' => '_expiration-date',
            );
$query = new WP_Query( $args ); ?>

<?php if($query->have_posts()) : ?>
   <?php while($query->have_posts()) : $query->the_post() ?>

         <?php get_template_part( 'content', get_post_format() ); ?>

   <?php endwhile ?>
<?php endif ?>

With the above code I can show posts I add an expiration date , regardless of the date on which it expires , now want to know how I can delete them in a loop by comparing the date of expiration to the current date.

Upvotes: 1

Views: 604

Answers (1)

bhavesh vala
bhavesh vala

Reputation: 873

Try this, A Code Work is in my web site, first your _expiration-date key value format is Y-m-d means 2015-10-17 must

<?php
$today = date("Y-m-d");
$args = array(
    'posts_per_page'   => -1,       
    'orderby'          => 'date',
    'order'            => 'DESC',       
    'meta_key'         => 'clx_event_start_date',
    'meta_query'  => array(
         array(         // restrict posts based on meta values
          'key'     => '_expiration-date',  // which meta to query
          'value'   => $today,  // value for comparison
          'compare' => '>='   // method of comparison              
     )
    'post_type'        => 'post', //your post type
    'post_status'      => 'publish'     
);
$posts_array = get_posts( $args );

foreach($posts_array as $RowCron){      
    echo $post_id = $RowCron->ID; 
}
?>

Upvotes: 1

Related Questions