2012-08-04 122 views
0

我正在開發我的網站並需要在發佈時隨時更改帖子作者ID。在發佈帖子時更改wordpress帖子作者

所有的職位將是編寫和管理,但由不同的作者提供的內容發佈和需要從管理員更改帖子作者ID與其他作者(作者ID將自定義字段獲得)

那麼如何做到這一點在發佈時即時發佈。

作者信息是否有來自自定義字段?因爲您可以手動在UI中完成此操作。

  • 創建後

  • 變化,筆者在下拉菜單中

這裏時,啓用在顯示選項「作者」塊是我的主題metabox.php代碼我正在使用wpalchemy metabox腳本,下面的代碼在我的cpt循環php文件中

// getting custom field value from image_artist 
// this is giving value like artist_login_name/id 
$png_gallery_meta->the_field('image_artist'); 
$artist_info = $png_gallery_meta->get_the_value(); 

// to separate artist_login_name and id 
$string = $artist_info; 
$artist = substr($string, 0, stripos($string, "/")); 

// getting first name and last name from user id 
$author_first_name = get_userdata(basename($string))->first_name; 

$author_last_name = get_userdata(basename($string))->last_name; 

所以我使用上面的代碼從自定義字段下拉選擇框中獲取用戶信息。現在我怎麼能使用上面的值動態更改ID,而管理員發佈帖子後,作者ID應該改變與上面的id值從選擇框中獲得。我希望現在更清楚。

回答

1
$post_id = $post->ID; // change this to whathever 
$user_id = '4'; // change this too 

$the_post = array(); 
$the_post['ID'] = $post_id; 
$the_post['post_author'] = $user_id; 

wp_insert_post($the_post); 

上面的代碼會更新當前通過後(通過後$> ID)環帶的4

ID作家閱讀如何wp_insert_post工作here。基本上,如果你傳遞了一個已經存在的帖子的ID,它會用你傳遞給它的新信息更新該帖子。 (在這種情況下,作者ID)

如果你想從您可以使用自定義字段搶東西:上custom fields

$author = get_post_meta($this->ID, 'author_id', TRUE); 

更多信息。

+0

非常感謝您的代碼,但仍很困惑。如何正確使用我的代碼以及在哪個文件中我應該放置wp_insert_post代碼以使其工作。請參考我的代碼更新的問題,如果你能以特定的方式幫助我。我真的很感激 – 2012-08-05 04:57:18

1

作者信息是否有來自自定義字段?因爲您可以手動在UI中完成此操作。

  • 創建後

  • 變化,筆者在下拉菜單中

這裏時,啓用在顯示選項「作者」塊是我的主題metabox.php代碼我正在使用wpalchemy metabox腳本,下面的代碼在我的cpt循環php文件中

// getting custom field value from image_artist 
// this is giving value like artist_login_name/id 
$png_gallery_meta->the_field('image_artist'); 
$artist_info = $png_gallery_meta->get_the_value(); 

// to separate artist_login_name and id 
$string = $artist_info; 
$artist = substr($string, 0, stripos($string, "/")); 

// getting first name and last name from user id 
$author_first_name = get_userdata(basename($string))->first_name; 

$author_last_name = get_userdata(basename($string))->last_name; 

所以我使用上面的代碼從自定義字段下拉選擇框中獲取用戶信息。現在我怎麼能使用上面的值動態更改ID,而管理員發佈帖子後,作者ID應該改變與上面的id值從選擇框中獲得。我希望現在更清楚。

相關問題