我正在尝试插入帖子/页面,然后将帖子ID保存为选项,但该选项不会保存到数据库中。
我可以创建页面并获取它的ID,但我无法将该ID保存到选项中。该类在“init”调用,构造函数如下:
class Recipe{
public function __construct(){
$favorites_id = get_option("favoritesid");
if($favorites_id <= 0){
$my_post = array(
'post_title' => 'Favorites',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
$result_id = wp_insert_post( $my_post );
if($result_id > 0){
add_option("favoritesid", $result_id);
}
}
}
答案 0 :(得分:1)
测试主插件文件中的代码:(//use array_push for option)
class Recipe{
public function __construct(){
add_action('init',[$this,'test']);
}
public function test(){
$favorites_id = get_option("favoritesid",[]);
if($favorites_id >= 0){
$my_post = array(
'post_title' => 'this is test',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
$result= get_page_by_title($my_post['post_title'],ARRAY_A, 'page');
if(is_null($result)) {
$result_id = wp_insert_post( $my_post );
if ( $result_id >= 0 ) {
update_option( "favoritesid", $result_id );
}
}
else return false;
}
}
}
new Recipe;