Thursday, October 17, 2013

Create / Retrieve / Update / Delete POST

Create Post

	function wpcreatepost(){
		$title = $_REQUEST['title'];
		$content = $_REQUEST['content'];
		$userid = $_REQUEST['userid'];
		$my_post = array(
			  'post_title'    => wp_strip_all_tags($title ),
			  'post_content'  => $content,
			  'post_status'   => 'publish',
			  'post_author'   => $userid,
			  'post_type'     => 'post',
		);
		$pid = wp_insert_post( $my_post );
		echo $pid;
		die();
	} 
 
 

Retrieve Post

function wpeditpost(){ $postid = $_REQUEST['postid']; $post = get_post( $postid, ARRAY_A); echo json_encode($post); die(); }


Update Post

	function wpupdatepost(){
		$title = $_REQUEST['title'];
		$content = $_REQUEST['content'];
		$postid = $_REQUEST['postid'];
	
		$my_post = array();
		$my_post['ID'] = $postid;
		$my_post['post_title'] = wp_strip_all_tags($title);
		$my_post['post_content'] = $content;
	
		// Update the post into the database
		wp_update_post( $my_post );
		die();
	} 
 

Delete Post

function wpdeletepost(){ $postid = rawurldecode($_REQUEST['postid']); $postids = rawurldecode($_REQUEST['postids']); if(!empty($postid)){ echo wp_delete_post($postid); }else if(!empty($postids)){ $postids = explode(",",$postids); foreach($postids as $id){ echo wp_delete_post($id); } } die(); }
 

 

Share This!


No comments:

Post a Comment

Powered By Blogger · Designed By Seo Blogger Templates