Skip to content

Instantly share code, notes, and snippets.

@davemac
Created November 27, 2016 07:35
Show Gist options
  • Save davemac/9cce4883cf4b8d8c06c0c23e78f3c5ff to your computer and use it in GitHub Desktop.
Save davemac/9cce4883cf4b8d8c06c0c23e78f3c5ff to your computer and use it in GitHub Desktop.
WP upload media and create post using media as featured image
add_action('add_attachment', 'create_post');
function create_post( $attach_ID ) {
$attachment = get_post( $attach_ID );
$my_post_data = array(
'post_title' => $attachment->post_title,
'post_type' => 'dmc-photo',
'post_category' => array('0'),
'post_status' => 'publish'
);
$post_id = wp_insert_post( $my_post_data );
// attach media to post
wp_update_post( array(
'ID' => $attach_ID,
'post_parent' => $post_id,
) );
set_post_thumbnail( $post_id, $attach_ID );
// wp_set_object_terms( $post_id, 9, 'photocat' );
return $attach_ID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment