当post状态更改为“已发布”时,我正在尝试更新该字段。当我检查what update字段返回时,它正确地获取了值,但没有更新。
它显示了一些数字。
请帮忙,我不确定我是否应该将此与transition_post_status
<小时>Update - I have tried below hooks // 正在触发操作,但字段未更新
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
}
最合适的回答,由SO网友:Sally CJ 整理而成
所以我安装了Yoast SEO plugin, 并测试了你的代码,现在我可以肯定地说“neither no nor yes, but you could“对于这个问题:
请帮忙,我不确定我是否应该将此与transition_post_status
transition_post_status
在wp_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
\' 工作了吗?
Why did I say neither no nor yes, but you could?
因为你可以使用
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版进行了尝试和测试。