Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mzdebo/87ba45ae901cc6d93dc505c624a5cdc8 to your computer and use it in GitHub Desktop.
Save mzdebo/87ba45ae901cc6d93dc505c624a5cdc8 to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////////////////////////////////////////
// //
// Create a post from every image in wordpress media gallery //
// //
////////////////////////////////////////////////////////////////////////////////////
//If you have the image in your media library you can just loop through them and create post via wp_insert_post.
// using wp_insert_post() function
// snippet from the Codex
// $post = array(
// 'ID' => [ <post id> ] // Are you updating an existing post?
// 'post_content' => [ <string> ] // The full text of the post.
// 'post_name' => [ <string> ] // The name (slug) for your post
// 'post_title' => [ <string> ] // The title of your post.
// 'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] // Default 'draft'.
// 'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] // Default 'post'.
// 'post_author' => [ <user ID> ] // The user ID number of the author. Default is the current user ID.
// 'ping_status' => [ 'closed' | 'open' ] // Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'.
// 'post_parent' => [ <post ID> ] // Sets the parent of the new post, if any. Default 0.
// 'menu_order' => [ <order> ] // If new post is a page, sets the order in which it should appear in supported menus. Default 0.
// 'to_ping' => // Space or carriage return-separated list of URLs to ping. Default empty string.
// 'pinged' => // Space or carriage return-separated list of URLs that have been pinged. Default empty string.
// 'post_password' => [ <string> ] // Password for post, if any. Default empty string.
// 'guid' => // Skip this and let Wordpress handle it, usually.
// 'post_content_filtered' => // Skip this and let Wordpress handle it, usually.
// 'post_excerpt' => [ <string> ] // For all your post excerpt needs.
// 'post_date' => [ Y-m-d H:i:s ] // The time post was made.
// 'post_date_gmt' => [ Y-m-d H:i:s ] // The time post was made, in GMT.
// 'comment_status' => [ 'closed' | 'open' ] // Default is the option 'default_comment_status', or 'closed'.
// 'post_category' => [ array(<category id>, ...) ] // Default empty.
// 'tags_input' => [ '<tag>, <tag>, ...' | array ] // Default empty.
// 'tax_input' => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty.
// 'page_template' => [ <string> ] // Requires name of template file, eg template.php. Default empty.
// );
//wp_insert_post($new_post);
//end snippet options
// code starts here, enter in functions or child theme
function import_post_from_imgs() {
$images = get_posts('post_type=attachment&post_status=inherit&posts_per_page=-1');
// just a minimal security check
if ( ! current_user_can('publish_posts') ) return;
if ( ! empty($images) ) { foreach ( $images as $image) {
// prevent duplicate if for some reason the function is called more than once
if ( get_post_meta($image->ID, '_imported', true) ) continue;
$post = array(
'post_title' => $image->post_title,
'post_content' => '',
'post_content' => 'publish',
'post_status' => 'draft', // Default 'draft'.
'post_type' => 'ouipinupgallery' // Default 'post'.
);
// insert post
$postid = wp_insert_post( $post );
if ( $postid ) {
// set the image as thumbnail
set_post_thumbnail($postid, $image->ID);
update_post_meta($image->ID, '_imported', 1);
}
} }
}
function go_import_post_from_imgs() {
if ( isset($_GET['import_images']) ) import_post_from_imgs();
}
add_action('admin_init', 'go_import_post_from_imgs');
// NOTES
In code above the import function is triggered on admin init, when the $_GET variable 'import_images' is setted.
So, you have to login into your dashboard and then the url of your page is sonething like http://example.com/wp-admin/. Now jus manually add '?import_images=1' so your url became http://example.com/wp-admin/?import_images=1 and hit Enter.
After some seconds you should see the posts created from images.
Be aware that this function create a post from all images you have updated.
If you want exclude some images, you can take 2 ways:
1. before if ( get_post_meta($image->ID, '_imported', true) ) continue;
look for the IDs of the images you want to exclude and add this 2 lines:
$exclude = array(12, 256, 587); // the ids you want to skip
if ( in_array($image->ID, $exclude) ) continue;
2. Previous method is good if you want to exclude a little number of images, if you want to exclude more, you can register a custom taxonomy for the attachments and assign a particular term to images you want to skip (e.g. 'skip').
See https://github.com/mzdebo/GMMediaTags to add buk function.
After that, Assuming the taxonomy is called 'media-tag' and you have added the 'skip' term to the images you want to skip,
add before if ( get_post_meta($image->ID, '_imported', true) ) continue;
add this line:
if ( has_term('skip', 'media-tag', $image->ID) ) continue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment