Delete post through meta_value Wordpress

2

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

In addition, I would like to make all expired posts appear at a certain point on my site.

I've tried using 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 display the posts that I add an expiration date, regardless of the date it expires, I now want to know how I can delete them in a loop by comparing their expiration date with the current date.     

asked by anonymous 08.10.2015 / 20:33

2 answers

1

There are several ways to do this. One of them is comparing meta_values , which I believe is what you're doing. I already suggest a slightly different approach. Note that the latest version of Post Expirator allows you to define what happens to a post after expiration, including the addition of a category, as shown below:

Isuggestyoucreatethecategory"Expired", or equivalent, and once a post expires, it gets this category. This makes your life easier (and a lot) at the time of the WP Loop. Just get the ID of the Category "Expired" through your slug (expired), as follows:

$id_expirados = get_category_by_slug( 'expirados' );

And make the loop more or less how you're doing

$args = (array( 'category__not_in' => $id_expirados );

$query = new WP_Query( $args ); ?>

if($query->have_posts()) :
   while($query->have_posts()) : 
       $query->the_post();

       get_template_part( 'content', get_post_format() );

   endwhile;
endif;

This excludes all posts in this category, and shows all others. To show only the already expired, the change would be

$args = (array( 'category__in' => $id_expirados );

So, only expired ones will be shown.

I think using this plugin is the smartest solution. If you do not want to deal with categories, and choose to do direct queries to meta_values , things change a bit more.

    
08.10.2015 / 21:28
1

This question is old but I will document here a way of doing that is independent of creating categories as suggested in the other answer. The PostExpirator plugin stores the expiration dates in the timestamp format ('U' ) , so we can filter only posts that have longer expiration dates than the current one using pre_get_posts , for example:

add_action( 'pre_get_posts', 'remove_expirados' );

function remove_expirados( $query ) {

    // rodar apenas na home page
    if ( is_front_page() ) {

        $meta_query = array(
            array(
                'key'     => '_expiration-date', // busca posts com datas de expiração
                'type'    => 'NUMERIC',          // compara numericamente
                'compare' => '>',                // buscando valores maiores
                'value'   => time(),             // que a hora atual
            ),
        );

        $query->set( 'meta_query', $meta_query );
    }
}
    
03.08.2017 / 14:13