0

I run a network of sites, with hundreds of editors, and lately we've had image copyright trolls coming after us. Most of these images are a year old or older, so we're looking to remove all images from posts older than 6 months.

Currently, I've written a plugin that removes images from the post content using SQL queries and runs on a daily cron. The only thing I can't figure out, and it's causing me to beat my head against my desk, is the removal of featured images on these posts. The following works, if done manually:

delete_post_thumbnail( POST ID HERE );

But, if I run a query with a foreach loop, it doesn't work. So, here's what I have that isn't working at all:

function featured_image_removal() {
  $args = array(
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'date_query'         => array(
      'column' => 'post_date',
      'after'  => '-180 days'
    )
  );

  $posts = get_posts($args);

  foreach($posts as $post) {
    delete_post_thumbnail($post->ID);
  }
}

My function, featured_image_removal, then runs through the cron. And the cron is working, because my other function, which removes images from the post content, is working perfectly fine.

Any ideas why this isn't working? It's probably right in front of my face and I've been looking at it so long, I'm not seeing it. Any help would be greatly appreciated! Help me stop banging my head on my desk. :D

  • date_query should be an array of an array – Pieter Goosen Apr 13 '16 at 16:15
0

I think you might be jumping the gun with actively discarding the data here. What if next month trolls go away, you change your mind, and want all those featured images back?

I would just suppress them on front end by editing template and making thumbnail output conditional or filtering API with something like this:

if ( ! is_admin() ) {

    add_filter( 'get_post_metadata', function ( $null, $post_id, $meta_key ) {

        if ( '_thumbnail_id' === $meta_key ) { // add check for date or whatever
            return false;
        }

        return $null;
    }, 10, 3 );
}

This way you retain all the existing data, just prevent featured images from showing. If you change your mind just remove the check and everything is back.

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.