admin管理员组文章数量:1130349
After I publish a post I want to get a custom field from the plugin advanced custom fields and store it inside a database. tried this:
function call_after_post_publish($post_id, $post) {
$tcParentTitle = get_the_title( $post_id );
$tcChildTitle = get_field( 'funcion_1_titulo_tc', $post_id );
global $wpdb;
$wpdb->insert(
'link',
array(
'parent_title' => $tcParentTitle,
'title' => $tcChildTitle,
'parent_id' => $post_id,
),
array(
'%s',
'%s',
'%d'
)
);
}
add_action( 'publish_post', 'call_after_post_publish', 10, 2 );
This doesn't seem to work because I think the function get_field() works only after the post is created. Is there other way I can get the custom field value?
After I publish a post I want to get a custom field from the plugin advanced custom fields and store it inside a database. tried this:
function call_after_post_publish($post_id, $post) {
$tcParentTitle = get_the_title( $post_id );
$tcChildTitle = get_field( 'funcion_1_titulo_tc', $post_id );
global $wpdb;
$wpdb->insert(
'link',
array(
'parent_title' => $tcParentTitle,
'title' => $tcChildTitle,
'parent_id' => $post_id,
),
array(
'%s',
'%s',
'%d'
)
);
}
add_action( 'publish_post', 'call_after_post_publish', 10, 2 );
This doesn't seem to work because I think the function get_field() works only after the post is created. Is there other way I can get the custom field value?
Share Improve this question edited Oct 25, 2018 at 2:25 Damian Leeron asked Oct 25, 2018 at 1:54 Damian LeeronDamian Leeron 112 bronze badges 2 |1 Answer
Reset to default 0I would suggest to use wp_insert_post hook to make sure that everything is processed before you hook in.
add_action( 'wp_insert_post', 'my_wp_insert_post_cb', 10, 2 );
function my_wp_insert_post_cb( $post_id, $post ) {
// Do your stuff here
}
After I publish a post I want to get a custom field from the plugin advanced custom fields and store it inside a database. tried this:
function call_after_post_publish($post_id, $post) {
$tcParentTitle = get_the_title( $post_id );
$tcChildTitle = get_field( 'funcion_1_titulo_tc', $post_id );
global $wpdb;
$wpdb->insert(
'link',
array(
'parent_title' => $tcParentTitle,
'title' => $tcChildTitle,
'parent_id' => $post_id,
),
array(
'%s',
'%s',
'%d'
)
);
}
add_action( 'publish_post', 'call_after_post_publish', 10, 2 );
This doesn't seem to work because I think the function get_field() works only after the post is created. Is there other way I can get the custom field value?
After I publish a post I want to get a custom field from the plugin advanced custom fields and store it inside a database. tried this:
function call_after_post_publish($post_id, $post) {
$tcParentTitle = get_the_title( $post_id );
$tcChildTitle = get_field( 'funcion_1_titulo_tc', $post_id );
global $wpdb;
$wpdb->insert(
'link',
array(
'parent_title' => $tcParentTitle,
'title' => $tcChildTitle,
'parent_id' => $post_id,
),
array(
'%s',
'%s',
'%d'
)
);
}
add_action( 'publish_post', 'call_after_post_publish', 10, 2 );
This doesn't seem to work because I think the function get_field() works only after the post is created. Is there other way I can get the custom field value?
Share Improve this question edited Oct 25, 2018 at 2:25 Damian Leeron asked Oct 25, 2018 at 1:54 Damian LeeronDamian Leeron 112 bronze badges 2-
Look in
$_POSTfor data or use theacf/save_postaction instead. – Milo Commented Oct 25, 2018 at 3:59 -
1
The hook
publish_postis fired beforesave_postthat's used by ACF. So it's better to hook intosave_postwith higher priority value. – ManzoorWani Commented Oct 25, 2018 at 5:35
1 Answer
Reset to default 0I would suggest to use wp_insert_post hook to make sure that everything is processed before you hook in.
add_action( 'wp_insert_post', 'my_wp_insert_post_cb', 10, 2 );
function my_wp_insert_post_cb( $post_id, $post ) {
// Do your stuff here
}
本文标签: Get an advanced custom field after post publish
版权声明:本文标题:Get an advanced custom field after post publish 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749233736a2336927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$_POSTfor data or use theacf/save_postaction instead. – Milo Commented Oct 25, 2018 at 3:59publish_postis fired beforesave_postthat's used by ACF. So it's better to hook intosave_postwith higher priority value. – ManzoorWani Commented Oct 25, 2018 at 5:35