0

I have an issue I can't seem to fold a solution for. I'm looking for a way to mass remove the featured images of posts older than X amount of days. 150 days is my target but if I can set it to whatever all the better.

I've been working in mySQL to do this but it seems the meta_value is too complex for my limited knowledge of regex and I can't seem to overcome it. And what I need is for the featured image on posts older than X amount of days (150 target) to simply not be there anymore; not in the code, seen, etc...

Right now I'm trying to replace it with a placeholder.png but that's not necessary as long as the featured image can just not be there.

Anyone run into an issue like this? Anyone overcome it? Can anyone help me overcome it?

I've tried a few plugins but to no avail. If you have one you know works please let me know.

It's imperative that the ones removed be only 150 days or older and not anymore recently.

Thank you everyone!

p.s. I want to add that my overall goal is to remove featured posts and post images older than 150 days. Sure I can just remove the files physically from the server but the code is still there. I just cannot have that.

Here's what I'm doing to remove post images. Can it be modified to safely remove featured images or is there a better way?

(201[0-5]\/(?:0[1-9]|1[0-2])\/.*?\.(?:jpg|png|gif|jpeg)) to be replaced with placeholder.png
  • Here's what I've come up with so far... but it doesn't work. function wpdev_remove_featured() { $args = array( 'post_type' => 'post', 'date_query' => array( 'after' => date('Y-m-d', strtotime('-180 days')) ) ); $posts = get_posts($args); foreach($posts as $post) { delete_post_thumbnail($post->ID); } } – Romulus Apr 12 '16 at 20:17
0

@Romulus

Since you are using Wordpress, I would highly suggest using Wordpress' functionality as much as you can. I would recommend using PHP and creating your own plugin or some kind of script that you can run. There are lots of tutorials out there that will give you the bare-bones implementation for creating a simple WordPress plugin. For now I'll run you through some helpful WordPress functions that should get you in the right direction.

There is a post from viper007bond.com that shows this snippet of code:

<?php
function last_thirty_days( $where = '' ){
    global $wpdb;
    $where .= $wpdb->prepare(" AND post_date > %s", date( 'Y-m-d', strtotime('-30 days'))
    return $where;
}
add_filter( 'posts_where', 'last_thirty_days');
$some_posts = get_posts( array( 'suppress_filters' => false) );
// Important to avoid modifying other queries
remove_filter( 'post_where', 'last_thirty_days');
?>

Though you will have to modify this snippet a bit to fit into your problem, it should suffice as a good starting point for you.

Once you've prepared the list of posts (which would be located in $some_posts) you can then loop through each of those posts and run the wordpress function delete_post_meta().

pseudo example:

foreach $post in $some_post loop{
   delete_post_meta($post->ID, '_thumbnail_id');
}

If you don't want to use filters for some reason you could just return all of your posts using $allposts = get_posts('numberposts=-1&post_type=post&post_status=any'); loop through all of those results comparing the date to the date you want. Once you've found the a post with the date your looking for run the delete_post_meta() on it like I mentioned before.

Hope this helps you!

I highly recommend this wordpress plugin for quickly testing PHP code: https://wordpress.org/plugins/wordpress-console/ It hasn't been updated in FOREVER but it still works if you don't use multisite.

  • Hmmm I'm not strong in PHP but (I hope) enough to figure this out. I can actually kinda see what it's doing and how to create the code. I'll see what I can come up with! Thank you! – Romulus Apr 12 '16 at 21:47
  • No problem. If you are used to other languages, then you will probably have no problem picking up PHP. – Myg0t Apr 12 '16 at 22:10

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.