投稿ステータスが[公開済み]に変更されたときにフィールドを更新しようとしています。値を正しく取得していますが、更新フィールドが何を返しているのかを確認すると更新されません。
それはいくつかの数を示しています。
transition_post_status
と一緒に使うべきかどうかわからないようにしてください
更新 - 以下のフックを試してみました//アクションは起動されていますが、フィールドは更新されていません
1。 publish//は動作しますが、更新は行われません
2。 save_post&&save_post_l//は動作しますが、更新は行われません。
add_action('transition_post_status', 'updated_to_publish', 10, 3);
function updated_to_publish($new_status, $old_status, $post)
{
if ($new_status == 'publish')) // I have removed the check for post type
{
$post_id = $post->ID;
if (get_field('advanced_option_edit_seo', $post_id)) {
if (defined('WPSEO_VERSION')) {
$metatitle = get_field('seo_title', $post_id);
$metadesc = get_field('seo_meta_description', $post_id);
$metakeywords = get_field('seo_keyword', $post_id);
update_post_meta($post_id, '_yoast_wpseo_title', $metatitle );
update_post_meta($post_id, '_yoast_wpseo_metadesc', $metadesc );
update_post_meta($post_id, '_yoast_wpseo_focuskw', $metakeywords);
}
}
} else {
return;
}
}
私は 'wp_insert_post
'アクションを使用して問題を解決することができました。他のアクションが失敗したのに「wp_insert_post
」が機能した理由を知ることはできますか?
テストに使用したコード
add_action('transition_post_status', 'updated_to_publish', 10, 3);
function updated_to_publish($new_status, $old_status, $post)
{
if (($new_status == 'publish') && ($post->post_type == 'l')) {
$post_id = $post->ID;
error_log( var_export( $post_id, 1 ) );
if (get_field('advanced_option_edit_seo', $post_id)) {
if (defined('WPSEO_VERSION')) {
// ACF field
$metatitle = get_field('seo_title', $post_id);
error_log( var_export( $metatitle, 1 ) );
$metadesc = get_field('seo_meta_description', $post_id);
error_log( var_export( $metadesc, 1 ) );
$metakeywords = get_field('seo_keyword', $post_id);
error_log( var_export( $metakeywords, 1 ) );
//plugin is activated
//old values
$metadesc_old = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
error_log( var_export( $metadesc_old, 1 ) );
$metatitle_old = get_post_meta($post->ID, '_yoast_wpseo_title', true);
error_log( var_export( $metatitle_old, 1 ) );
$metakeywords_old = get_post_meta($post->ID, '_yoast_wpseo_focuskw', true);
error_log( var_export( $metakeywords_old, 1 ) );
update_post_meta($post_id, '_yoast_wpseo_title', $metatitle, $metatitle_old);
error_log( var_export( $tyone, 1 ) );
update_post_meta($post_id, '_yoast_wpseo_metadesc', $metadesc, $metadesc_old);
error_log( var_export( $tytwo, 1 ) );
update_post_meta($post_id, '_yoast_wpseo_focuskw', $metakeywords, $metakeywords_old);
error_log( var_export( $tythree, 1 ) );
}
}
} else {
return;
}
//Do something
}
そこで、私は Yoast SEOプラグイン をインストールし、コードをテストしたところ、「どちらともいえないが、できる」と積極的に言うことができました。 = "この質問へ:
これを
transition_post_status
と一緒に使用する必要があるかどうかわからないので助けてください
transition_post_status
が発生しますbeforewp_insert_post
アクションが発生し、Yoast SEOプラグインはwp_insert_post
アクションを介してすべてのカスタムフィールドを実際に保存(追加/更新)します:
// See WPSEO_Metabox::save_postdata() (in wordpress-seo/admin/metabox/class-metabox.php)
add_action( 'wp_insert_post', array( $this, 'save_postdata' ) );
したがって、コード自体が機能し、フィールドが更新されます(新しい値と現在の値が同じではなく、もちろんすべてのif
条件が満たされている場合)。ただし、Yoast SEOプラグインはWPSEO_Metabox::save_postdata()
関数を介して値をオーバーライドします。これにより、この質問に答えることができます。
'
wp_insert_post
'アクションを使用して問題を解決できました。他のアクションが失敗したが「wp_insert_post
」が機能した理由を知ることができますか?
なぜ私が言ったのはノーでもイエスでもないが、あなたはできる?
transition_post_status
とともにwp_insert_post
を使用できるため、次のようになります。
add_action( 'transition_post_status', 'do_updated_to_publish', 10, 2 );
function do_updated_to_publish( $new_status, $old_status ) {
if ( $new_status !== $old_status && 'publish' === $new_status ) {
add_action( 'wp_insert_post', 'updated_to_publish', 10, 2 );
}
}
function updated_to_publish( $post_id, $post ) {
// Remove it; it will be re-added via the do_updated_to_publish() function,
// if necessary or when applicable.
remove_action( 'wp_insert_post', 'updated_to_publish', 10, 2 );
if ( ! defined( 'WPSEO_VERSION' ) || 'l' !== $post->post_type ) {
return;
}
if ( get_field( 'advanced_option_edit_seo', $post_id ) ) {
// Make your update_post_meta() calls here.
update_post_meta( $post_id, '_yoast_wpseo_focuskw', 'test' );
error_log( 'focuskw updated for post #' . $post_id );
}
}
Yoast SEOバージョン9.1での動作を試し、テストしました。
元の関数は、適切な値に設定されている$ _POST ['post_type']に依存しています。一般的な規則として、あなたはグローバル変数の使用を避けるべきです - もしあなたが関数があなたに与えたいだけであれば、あなたはそれが呼ばれるべきである文脈について考える必要はありません。
この場合、それが起こります。あなたは関数がグローバル変数$ _POST ['post_type']に依存しています、そしてそれは1つの 'state'(投稿を公開)では機能しますが別のものでは機能しません(cronジョブ、投稿の更新)。一言で言えば、$ _POST ['post_type']はいつもあなたがそれがあるべきであると思うものではありません。
以下は、渡された$ post変数から投稿タイプを取得します。
add_action('transition_post_status', 'updated_to_publish', 10, 3);
関数updated_to_publish($ new_status、$ old_status、$ post){
if (($new_status == 'publish') && (get_post_type( $post ) == 'l')) {
$post_id = $post->ID;
if (get_field('advanced_option_edit_seo', $post_id)) {
if (defined('WPSEO_VERSION')) {
//Code goes here
}
}
} else {
return;
}
}
私が答えをコピーしたところからこのリンク にアクセスしてください。
wpseo_titleを試してくださいwpseo_titleを更新するためのフィルタ。
add_filter('wpseo_title', 'filter_product_wpseo_title');
function filter_product_wpseo_title($title) {
global $post;
$post_id = $post->ID;
if (get_field('advanced_option_edit_seo', $post_id)) {
if (defined('WPSEO_VERSION')) {
$title = get_field('seo_title', $post_id);
}
}
return $title;
}
より多くのseoデータ更新の例については: https://yoast.com/wordpress/plugins/seo/api/