php - how to add posts with image programmatically in wordpress -
this code add post programmatically in wordpress
require(dirname(__file__) . '/wp-load.php'); global $user_id; $new_post = array( 'post_title' => 'table tennis', 'post_content' => 'table tennis or ping-pong sport in 2 or 4 players hit lightweight ball , forth using table tennis racket. game takes place on hard table divided net. except initial serve, players must allow ball played toward them 1 bounce on side of table , must return bounces on opposite side. points scored when player fails return ball within rules.', 'post_status' => 'publish', 'post_date' => date('y-m-d h:i:s'), 'post_author' => $user_id, 'post_type' => 'post', 'post_category' => array(2), ); $post_id = wp_insert_post($new_post);
how add image post?
i new wordpress,thanks in advance..
this upload file using wordpress , insert on post featured image.
$wp_filetype = wp_check_filetype($filename, null); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => $filename, 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $thumbnail, $post_id ); // must first include image.php file // function wp_generate_attachment_metadata() work require_once(abspath . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $thumbnail ); wp_update_attachment_metadata( $attach_id, $attach_data ); // add featured image post add_post_meta($post_id, '_thumbnail_id', $attach_id);
Comments
Post a Comment