IM正在处理一个小插件,该插件会激活时创建一个主题页面,然后一个函数将此页面设置为发布,这是我发布的代码:

// function that creates the new ads page on plugin install // 
function mjj_create_page ()
{
// Create new page object
$ads_page = get_option('mjj_smart_ads_page');

$ads_new_page = array(
    'post_title' => 'Smart Ads',
    'post_content' => '',
    'post_status' => 'publish',
    'post_type' => 'page'  
 );   
// Insert the page into the database
$ads_page = wp_insert_post( $ads_new_page );
update_option('mjj_smart_ads_page', $ads_page);

// now lets give this new page a groovy template
$ads_page_data = get_page_by_title('Smart Ads');
$ads_page_id = $ads_page_data->ID;
update_post_meta($ads_page_id, '_wp_page_template','tpl-smart-ads.php');

}

不知道它的语义WP是否完美,但它似乎可以正常工作并且可以按照我的需求,但是现在我试图设置插件上的函数,该函数将停用此创建的页面以草稿状态,以便它在菜单中不显示,但是它只是dobt似乎想玩,这是我正在与之合作的事情:

// function that drafts smart ads page on plugin deactivate // 
function mjj_unpublish_page ()
{
$old_ads_page = get_option('mjj_old_smart_ads_page'); 

$ads_old_page = array(
    'post_title' => 'Smart Ads',
    'post_content' => '',
    'post_status' => 'draft',
    'post_type' => 'page'  
  );
     // Insert the page into the database
$old_ads_page = wp_update_post( $ads_old_page );
update_option('mjj_old_smart_ads_page', $old_ads_page); 
}

下面是我的钩子

    // create the page to get the info for selling ads and posting ads
    register_activation_hook($file, array(&$this, 'mjj_create_page'));
    //while in this block i will also add the deactivate function to unpublish the created page
register_deactivation_hook($file, array(&$this, 'mjj_unpublish_page'));

如您所见,我认为要草稿的设置将包括使用WP_UPDATE_POST,但是停用时它似乎并不正确

有帮助吗?

解决方案

我将此作为您及其基于页面ID的另一个解决方案

/*
$post_id - The ID of the post you'd like to change.
$status -  The post status publish|pending|draft|private|static|object|attachment|inherit|future|trash.
*/
function change_post_status($post_id,$status){
    $current_post = get_post( $post_id, 'ARRAY_A' );
    $current_post['post_status'] = $status;
    return wp_update_post($current_post);
}

因此,一旦拥有此功能,就可以将其与您创建的页面的页面ID一起使用:

$ads_page = get_option('mjj_smart_ads_page');
$old_ads_page = change_post_status($ads_page,'draft');
update_option('mjj_old_smart_ads_page', $old_ads_page); 
许可以下: CC-BY-SA归因
scroll top