最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Get an advanced custom field after post publish

matteradmin8PV0评论

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 $_POST for data or use the acf/save_post action instead. – Milo Commented Oct 25, 2018 at 3:59
  • 1 The hook publish_post is fired before save_post that's used by ACF. So it's better to hook into save_post with higher priority value. – ManzoorWani Commented Oct 25, 2018 at 5:35
Add a comment  | 

1 Answer 1

Reset to default 0

I 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
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far