PHPで作成したばかりの投稿の投稿者を設定するにはどうすればよいですか?
-
22-10-2019 - |
質問
私はPHPを使用してカスタム投稿を動的に作成しています。著者がユーザーに記録されている以外の誰かである必要があります。私はこれを見つけました https://stackoverflow.com/questions/5759359/wordpress-manually-set-the-outhor-of-a-post-in-php しかし、投稿が既に挿入された後にそれを行う方法があるかどうか疑問に思っています。私はDBクエリをすることができると思います...
解決
著者のIDがわかっている場合は、wp_insert_postを使用して、IDと著者IDを指定することができます。
$id = $post->ID; // change this to whathever
$user_id = '4'; // change this too
$the_post = array();
$the_post['ID'] = $id;
$the_post['post_author'] = $user_id;
wp_insert_post( $the_post );
トリックは、IDを指定して投稿を更新することです。見る wp_insert_post()
.
他のヒント
この質問と別の質問との間の簡単さと関連性のために尋ねられた スタックオーバーフロー (WordPress- PHPの投稿の著者を手動で設定する - ここでopによってリンクされているように。)。
WordPressは値を強制しているようです post_author
使用を使用して投稿を挿入または更新している間 wp_insert_post()
と wp_update_post()
.
それを回避する方法は、フィルターフックを使用することです wp_insert_post_data
.
/**
* Filter slashed post data just before it is inserted into the database.
*
* @since 2.7.0
*
* @param array $data An array of slashed post data.
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
*/
$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
フィルターフックの使用例 wp_insert_post_data
:
function remove_author_id( $data, $postarr ) {
if ( $data['post_type'] != 'YOUR-POST-TYPE-HERE' ) {
return $data;
}
$data['post_author'] = 0;
return $data;
}
add_filter( 'wp_insert_post_data', 'remove_author_id', '99', 2 );
これは、投稿を使用して投稿を挿入または更新するのに特に便利です PHP
.
ノート: あなたは確実にサポートを無効にしたいと思うでしょう author
カスタム投稿タイプでは、おそらくこの投稿タイプの範囲内で著者関連の機能を使用して慎重になります。
これがカスタム投稿タイプであり、著者が投稿に割り当てられたくない場合は、「著者」を削除できます supports( array )
register_post_typeで。 http://codex.wordpress.org/function_reference/register_post_type
ポストタイプの著者サポートがまだ必要な場合、これは、著者のMetaboxをフィルタリングすることにより、post.php / post-new.phpでこれを行うだけではるかに理にかなっています。
解決策は、wp_dropdown_usersを使用して、noneまたはnullユーザーをドロップダウンに追加することです 'show_option_none'
WordPressが使用されます <option value="-1">
nullユーザーの場合、dbに0として表示されます。
*注:この例では、著者Divを公開ボタンのすぐ上に移動します。
add_action( 'post_submitbox_misc_actions', 'move_author_meta' );
function move_author_meta() {
global $post_ID;
$post = get_post( $post_ID );
echo '<div id="author" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">Author: ';
better_author_meta_box( $post ); //This function is being called in replace author_meta_box()
echo '</div>';
}
function better_author_meta_box($post) { ?>
<label class="screen-reader-text" for="post_author_override"><?php _e('Author'); ?></label>
<?php
if ( 'auto-draft' == $post->post_status ) : $selected = false; elseif ( $post->post_author == 0 || ( ! $post->post_author) ) : $selected = -1; else : $selected = $post->post_author; endif;
wp_dropdown_users( array(
'who' => 'authors',
'name' => 'post_author_override',
'selected' => $selected ? $selected : (int) -1,
'include_selected' => true,
'show_option_none' => 'NONE',
'orderby' => 'display_name',
'show' => 'display_name',
'order' => 'ASC'
) );
}
選択した$の追加の条件付きチェックがすべて気づいていると確信しています。過剰になるかもしれませんが、編集者が以前に公開された投稿から著者を変更できないという問題を排除しました。