Delete post through meta_value Wordpress

Asked

Viewed 489 times

2

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

Also, I would like to have all expired posts displayed at a certain point on my site.

I’ve been trying to use 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 posts that add an expiration date, regardless of the date on which it expires, now I want to know how I can delete them in a loop comparing the date of their expiration with the current date.

  • Sincere doubt, the plugin is called Post Expirator and does not expire the post when doing a query? Some way it has to do the correct query that does not show such expired posts, no?!

  • The option it provides after it expires is to delete the post, turn it into a draft, or move to some category. What I want is to keep the posts in the category where they were created, but at the bottom of the page, separate from posts that have not expired or have no expiration date.

  • Okay. It’s just that this post turning into a draft or changing category is not written in the question (it’s cool to add, and it’s just [Edit]). I don’t know how to solve that kind of problem search query as a starting point to find the solution: http://wordpress.stackexchange.com/search?tab=votes&q=%5bwp-query%5d%20meta_key%20date%20is%3aa

2 answers

1

There are several ways to do this. One of them is by comparing the 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, after the expiration, define what happens to a post, including adding a category, as shown below:

Categoria

I suggest you create the "Expired" category, or equivalent, and as soon as a post expires, it gets this category. This makes your life easier (and much easier) when it comes to performing the WP Loop. Just catch the ID of the "Expired" Category through your slug (expired) as follows:

$id_expirados = get_category_by_slug( 'expirados' );

And loop more or less like 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 deletes all posts in this category, and shows all others. To show only the expired ones, the change would be

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

Thus, only expired ones will be shown.

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

  • Thank you for your answer, Caio. Actually, creating the expired category was what I did, however I would like to keep the link of expired posts with their respective categories. With this, expired posts would be displayed in their original categories, only not prominently, at the top of the category page, but at the bottom, separate from posts that have not expired. In this case I think only with the same meta_values. The separation would be comparing the current date with the expiration date, but I really can’t find a light to do.

  • This is not so hard to do. I will take a look here and already edit the answer

  • @Rafael, I think we’re breaking up over meta_values will bring you exactly the same result. You will have expired posts and not expired posts. Adding a category to a post does not prevent it from being displayed on pages in other categories, unless you explicitly declare this exclusion. To display the expired ones below, but on the same page, I think the solution will be to make 2 loops on the same page. One without the expired ones and one with the expired ones

  • Then Felipe, but in the expired category would be all posts (mixed), there could not separate them and show them in their respective categories.

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 Post Expirator plugin saves expiration dates on timestamp format ('U'), then we can filter only posts that have expiration dates longer than the current one using the action 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 );
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.