Skip to content

Instantly share code, notes, and snippets.

@mayeenulislam
Created November 24, 2017 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayeenulislam/bf0367616c9cedbd7dddda036dae540f to your computer and use it in GitHub Desktop.
Save mayeenulislam/bf0367616c9cedbd7dddda036dae540f to your computer and use it in GitHub Desktop.
Set a single featured image to all the NanoSupport Knowledgebase posts on activation. Before activating the plugin set the default post_thumbnail_id on line #19
<?php
/**
* Plugin Name: Bulk Featured Image (KB)
* Description: Set a single featured image to all the NanoSupport Knowledgebase posts on activation. Before activating the plugin set the default post_thumbnail_id on line #19.
* Plugin URI: http://nanodesignsbd.com/
* Author: Mayeenul Islam
* Author URI: http://nanodesignsbd.com/
* Version: 1.0.0
* License: GPL2
*/
/**
* Add designated Same Featured Image to KB articles
* on Activation of the plugin.
*/
register_activation_hook( __FILE__, function() {
// Attachment ID of a common image for all the KB posts.
$thumbnail_id = 12; // <----------------- SET YOURS HERE
$kbposts = get_posts( array(
'post_type' => 'nanodoc', // KB posts only
'numberposts' => '-1' // get all of 'em
) );
// Go through all of 'em one by one.
foreach( $kbposts as $kbpost ) {
// If it already set one, don't do anyting.
if( ! has_post_thumbnail( $kbpost->ID ) ) {
// it has no featured image, set our default one to this one.
set_post_thumbnail( $kbpost->ID, $thumbnail_id );
}
}
});
/**
* FALLBACK :: IF ANYTHING GO WRONG
* Remove all the Featured Images from KB articles
* on DeActivation of the plugin.
*/
/*register_deactivation_hook( __FILE__, function() {
$kbposts = get_posts( array(
'post_type' => 'nanodoc', // KB posts only
'numberposts' => '-1' // get all of 'em
) );
// Go through all of 'em one by one.
foreach( $kbposts as $kbpost ) {
// If it already set one, don't do anyting.
if( has_post_thumbnail( $kbpost->ID ) ) {
// Delete featured images.
delete_post_thumbnail( $kbpost->ID );
}
}
});*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment