我正试图通过前端在定制的postype中创建一个post,这个post也是成功创建的。但是cutom permalink是空的,除非我进入仪表板并单击update。
$todaydate = new DateTime();
$todaydate =$todaydate->format('Y-m-d H-i-s');
$bookid="harrypotter";
$mypost= array('post_type'=>'books',
'post_title'=>$bookid,
'post_author'=>'1',
'post_status'=>'publish',
'post_date'=>$todaydate);
$wpdb->insert('wp_posts',$mypost);
$bookid=$wpdb->insert_id;
当我进入post类型的书籍时,我发现创建了这个帖子,但是permalink是http://localhost/mysite/newbooks//
而不是http://localhost/mysite/newbooks/harrypotter/
。
那么,如何让它也更新post的permalink?
发布于 2018-05-10 08:46:57
不要直接插入数据库。使用适当的函数,以便在插入数据库之前正确地创建帖子。
在您的例子中,正确的函数是wp_insert_post()
:
wp_insert_post( array(
'post_type' => 'books',
'post_title' => 'harrypotter',
'post_status' => 'publish',
'post_author' => 1,
) );
如果需要当前时间,则不需要手动设置日期字段。
https://wordpress.stackexchange.com/questions/303241
复制相似问题