How to create dyanmic post in wordpress – PHP code

How to create dyanmic post in wordpress

/* Create POST object*/

global $wpdb;

$cdl_post = array();
$cdl_post['post_title'] = "My Dynamic Post";
$cdl_post['post_content'] = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
$cdl_post['post_status'] = "publish";
$cdl_post['post_author'] = 1;
$cdl_post['post_cateogry'] = array(0);

/* Insert dynamic post into database*/
wp_inset_post( $cdl_post );

/* Get url of last inserted post from wp_posts table*/
$table_name_post = $wpdb->prefix . 'posts';
$result_lastpost = $wpdb->get_row("SELECT * FROM `$table_name_post` WHERE ID=(SELECT  MAX(`ID`) AS `post_id` FROM `$table_name_post`  WHERE `post_status`='publish')",OBJECT);

var_dump($result_lastpost->guid); // print last inserted post URL