admin管理员组文章数量:1431383
I'm building a website and I need to have the Yoast description programmatically added after saving a post (a custom post type in my case).
Now, since I'm using Advanced Custom Fields, and I also need some custom fields populated programmatically, I created a function in functions.php and called it this way:
add_action( 'acf/save_post', 'atc_set_element_intro', 20 );
...so it's fired every time a post is saved. This is the function:
function atc_set_element_intro( $post_id ){
if( get_post_type( $post_id ) == 'giochi' ){
// [CUT]
// Here I generate the strings ($intro and $intro_y) to fill the custom fields with
update_field( 'field_5d0be8f40addf', $intro, $post_id );
update_post_meta( $post_id, '_yoast_wpseo_metadesc', $intro_y );
} else {
return;
}
}
Now, the "update_field" works perfectly fine, and it adds a value on a custom field of my choice. The "update_post_meta" isn't working, unless I add a die() or wp_die() after it. In that case, I see a blank screen on save, and then checking my post I see that it added the Yoast description I wanted. But of course I can't die() on that function.
I guess there's something after the execution of that function that rewrites the Yoast description. What can I do to prevent that? Thank you.
I'm building a website and I need to have the Yoast description programmatically added after saving a post (a custom post type in my case).
Now, since I'm using Advanced Custom Fields, and I also need some custom fields populated programmatically, I created a function in functions.php and called it this way:
add_action( 'acf/save_post', 'atc_set_element_intro', 20 );
...so it's fired every time a post is saved. This is the function:
function atc_set_element_intro( $post_id ){
if( get_post_type( $post_id ) == 'giochi' ){
// [CUT]
// Here I generate the strings ($intro and $intro_y) to fill the custom fields with
update_field( 'field_5d0be8f40addf', $intro, $post_id );
update_post_meta( $post_id, '_yoast_wpseo_metadesc', $intro_y );
} else {
return;
}
}
Now, the "update_field" works perfectly fine, and it adds a value on a custom field of my choice. The "update_post_meta" isn't working, unless I add a die() or wp_die() after it. In that case, I see a blank screen on save, and then checking my post I see that it added the Yoast description I wanted. But of course I can't die() on that function.
I guess there's something after the execution of that function that rewrites the Yoast description. What can I do to prevent that? Thank you.
Share Improve this question asked Jul 11, 2019 at 17:25 Oni-LinkOni-Link 137 bronze badges1 Answer
Reset to default 1After days of trying I managed to fix it myself.
Instead of using update_post_meta, I just did this:
$_POST[ "yoast_wpseo_metadesc" ] = $intro_y;
It works perfectly.
Hope this can help someone.
本文标签: functionsProgrammatically add Yoast meta description after post save
版权声明:本文标题:functions - Programmatically add Yoast meta description after post save 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744653084a2617802.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论