I have this code which is meant to print text to a custom field.
The code works when I manually enter the post id like this example 4221
.
However, I need to fetch the post id using $wp_query->post->ID;
//works
$post_id = '4221';
$field_key = "field_607aa2cb60022";
$value ="some tring for testing";
update_field( $field_key, $value, $post_id );
//Dynamically, won't work
$post_id = "'$wp_query->post->ID;'";
$field_key = "field_607aa2cb60022";
$value = "some tring for testing";
update_field( $field_key, $value, $post_id );
I wrapped $wp_query->post->ID;
inside single and double quotes so as to return the number in single quotes '4221'. I can't get it to work without manually specifying the post id which is something I want to avoid and just use the current post id.
Not sure how to go about it.
Try to hook it to
wp_head
action hook.This code would go to your
functions.php
.