cwhiro
cwhiro

Reputation: 143

Set php code on function.php for action hook post update

I currently set below code on header.php. Purpose is updating custom field. It updates custom field when the post is opened by browser, and it works fine.

Now, I like to set this code on function.php, and run it when the post updated.

<?php if ( is_single() ) : ?>
<?php
$post_id = $post->ID;
if ( metadata_exists( 'post', $post->ID, '1aa'));
$modified = get_post_meta( $post->ID, '1aa', true );
update_post_meta($post->ID, '1aa', $modified);
?>
<?php endif; ?>

Code on function.php seems to have unfamiliar lines for me. Below code is something Im trying to write now. It would be appreciated if somebody indicate if something wrong.

<?php
function update_custom_field( $post_id );
if ( is_single() ) :{
    $post_id = $post->ID;
    if ( metadata_exists( 'post', $post->ID, '1aa'));
    $modified = get_post_meta( $post->ID, '1aa', true );
    update_post_meta($post->ID, '1aa', $modified);

endif;}
add_action( 'post_updated', 'update_custom_field' );
?>

I sometimes see some mysterious number 10 or 3 like below usage. Should I put this number too?

add_action( 'post_updated', 'update_custom_field', 10, 3 );

I made the code following nice advise.

function add_custom_field( $post_id, $post, $update ) {
if ( is_single() && metadata_exists( 'post', $post_id, '2a' ) ) :{
    $date = get_post_meta( $post_id, '2a', true );
    update_post_meta($post_id, '2a', $date );}
    else:{
    $date = get_the_date('Y-m-d', $post_id);    
    add_post_meta( $post_id, '2a', '$date', true );}
endif;
}

add_action( 'save_post', 'add_custom_field', 10, 3 );

However, this code does not effect on custom field '2a'. When I ver_dump it, it shows date I wanted like 2020-03-11.

Upvotes: 0

Views: 127

Answers (1)

disinfor
disinfor

Reputation: 11533

There are several problems in your PHP syntax, but mostly, your function isn't encapsulating anything and you are confusing if statement constructors (you only need : or { to open an if.

Here is what you are looking for:

function update_custom_field( $post_id, $post, $update ) {

    if ( is_single() && metadata_exists( 'post', $post_id, '1aa' ) ) :
        $modified = get_post_meta( $post_id, '1aa', true );
        update_post_meta($post_id, '1aa', $modified);
    endif;
}
add_action( 'save_post', 'update_custom_field', 10, 3 );

Basically, we use the $post_id argument that is passed into the function. This argument is the post id.

Next, we combine both if statements, no reason to have two.

Then we do all the checking and modify your post meta.

The add_action is outside of the function. It can be before or after it, I tend to put the action after. I changed the hook to save_post since this will check on save and update.

To your question about the numbers:

  1. The first number 10 is priority. That's when the action hook fires in relation to other functions on the same hook.

  2. The second number 3 is arguments. The save_post hook (and update_post) hook takes three arguments: post_id, post and update: https://developer.wordpress.org/reference/hooks/save_post/

Upvotes: 1

Related Questions