如标题中所述,我希望根据ACF自定义字段自动保存/更新post_excerpt和post_thumbnail的值(主要是出于与其他插件相符的原因)。现在,在完成这项工作的同时,我遇到了两个问题,首先是使用以下函数:
function test_FeaturedImageSetByACF() {
$current_screen = get_current_screen(); // Current admin screen needed to identify the current cpt
$current_cpt_name = $current_screen->post_type; // Current cpt name
$current_cpt_support = 'thumbnail'; // We want to check if the CPT supports this feature
$post_id = get_the_ID(); // Current post ID
$post_image_field = get_field('post_head_img'); // ACF field we want to sync
$post_image_id = $post_image_field['id']; // ACF image filed ID
$post_image_url = $post_image_field['url']; // ACF image filed URL
// If current cpt supports thumbnails/featured images
if ( post_type_supports( $current_cpt_name, $current_cpt_support ) ) {
if ( ( $post_image_url ) AND ( ( $post_image_url ) != ( get_the_post_thumbnail() ) ) ) {
delete_post_thumbnail( $post_id );
set_post_thumbnail( $post_id, $post_image_id );
}
}
}
add_action('save_post', 'test_FeaturedImageSetByACF', 13, 2 );
add_action('publish_post', 'test_FeaturedImageSetByACF', 10, 2 );
它确实有效,但有时它似乎只在第二次保存时更新值(这意味着我必须保存两次)。我知道我要么使用错误的钩子,要么使用错误的优先级或类似的东西,但我无法弄清楚它是哪一个。
我有第二个问题,我想为后期摘录做些类似的事情。现在这些函数与前一个函数看起来很相似,但我不知道要更新哪个值。有人能指出我正确的方向吗?
提前致谢
答案 0 :(得分:0)
您可以使用acf/save_post
挂钩。传递对象ID
add_action('acf/save_post', 'handle_acf', 20);
function handle_acf($object_id)
{
// Do your stuff
}
答案 1 :(得分:0)
由于这更像是一个ACF特定的问题,而不是一般的问题,我决定在ACF的支持论坛上发帖,用户John Huebner指出我正朝着正确的方向发展。对于任何感兴趣的人,可以在https://support.advancedcustomfields.com/forums/topic/set-wordpress-excerpt-and-post-thumbnail-based-on-custom-field/找到该主题(如果该帖子可能被删除或者其他内容),这是我用于摘录部分和自定义帖子缩略图/特色图像的代码:
add_action('save_post', 'flex_CustomExcerptSetByACF', 50);
function flex_CustomExcerptSetByACF() {
global $post;
$post_id = ( $post->ID ); // Current post ID
$post_excerpt = get_field( 'post_excerpt', $post_id ); // ACF field
if ( ( $post_id ) AND ( $post_excerpt ) ) {
$post_array = array(
'ID' => $post_id,
'post_excerpt' => $post_excerpt
);
remove_action('save_post', 'flex_CustomExcerptSetByACF', 50); // Unhook this function so it doesn't loop infinitely
wp_update_post( $post_array );
add_action( 'save_post', 'flex_CustomExcerptSetByACF', 50); // Re-hook this function
}
}
add_action('save_post', 'flex_FeaturedImageSetByACF', 50);
function flex_FeaturedImageSetByACF() {
$current_screen = get_current_screen(); // Current admin screen needed to identify the current cpt
$current_cpt_name = $current_screen->post_type; // Current cpt name
$current_cpt_support = 'thumbnail'; // We want to check if the CPT supports this feature
global $post;
$post_id = ( $post->ID ); // Current post ID
$post_image_field = get_field('post_head_img', $post_id ); // ACF field
if ( ( $post_id ) AND ( $post_image_field ) ) {
$post_image_id = $post_image_field['id']; // ACF image filed ID
$post_image_url = $post_image_field['url']; // ACF image filed URL
// If current cpt supports thumbnails/featured images
if ( post_type_supports( $current_cpt_name, $current_cpt_support ) ) {
if ( ( $post_image_url ) AND ( ( $post_image_url ) != ( get_the_post_thumbnail() ) ) ) {
update_post_meta($post_id, '_thumbnail_id', $post_image_id);
}
}
}
}