Support » Developing with WordPress » How to remove all posts that do not have a Featured images assigned to them?

  • Resolved loganivan

    (@loganivan)


    How to remove all posts that do not have a Featured images assigned to them?

    How to make a mini plugin for when I activate it, it deletes all posts without a featured images assigned to them?

    I trying:

    add_action( 'init', 'process_posts' );
    
    function process_posts() {
    
        $args = array(
          'meta_query' => array(
             array(
               'key' => '_thumbnail_id',
               'value' => '?',
               'compare' => 'NOT EXISTS'
             )
          ),
        );
    
        $new_query = new WP_Query( $args );
         if (empty($_thumbnail_id)) {
           wp_delete_post($_POST['post_id'], true);
    }
    }

    Can someone show this to me, please?. Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • <?php
    
    	add_action( 'init', 'process_posts' );
    
    	function process_posts() {
    
    	    $args = array(
    	    	'post_type' 		=> array( 'your-post-type-1', 'your-post-type-2', ... ),
    	    	'posts_per_page'	=> -1, 
    	    );
    
    	    $new_query = new WP_Query( $args );
    
    	    if( $new_query->found_posts ) {
    	    	foreach( $new_query->posts as $post ) {
    	    		if( !has_post_thumbnail( $post ) ) {
    			    wp_delete_post( $post->ID, true );
    			}	
    	    	}
    	    }
    	}
    
    ?>

    This link will help you to create a new plugin from scratch
    https://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/

    Thread Starter loganivan

    (@loganivan)

    Hey Friend @Soumanta_Bhowmick , thank you so much!! Worked Very well!! Great!!
    Thank you very much, had a plugin that did not work well when I needed to delete images out of wordpress, through the linux terminal, in case of emergency to free more disk space, and the posts were left without images in featured images, but in [box festured image] in post edit, said that I still had a picture!

    it was empty background, and with the name of the image that I deleted out of wordpress !! wordpress continued with featured images applied in post edit! there was an empty bottom! very strange! but the thumbnails in the posts, disappeared, so, as the wordpress system understood that I still had a featured image, my code did not work, but its works because it works with the thumbs! … thank you very much friends πŸ™‚ ..

    I liked to know this site that you indicated, I am creating a new blog and I will create a post indicating it! … I have only one doubt ?! because your code does not work with nginx! it drops the site the moment we activate the plugin! In apache everything happens ok … it was just a doubt .. not important .. your code already serves me very well and thank you very much πŸ™‚

    Glad it worked for you.
    Well, I have no experience with nginx as of now, so I can’t really tell the difference from apache.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to remove all posts that do not have a Featured images assigned to them?’ is closed to new replies.