Support

Account

Home Forums General Issues update_field issue

Solved

update_field issue

  • Hi there,

    I’m trying to display stocks on the front based on ‘change_percent’
    I’m getting the information from a json file, and so I created a custom field ‘ceg_valtozas’ for the change percentage. This I converted to absolute value, as I want to display the largest ones despite the change being positive or negative.

    I’m able to grab the numbers, the output is correct, however, for some reason I can’t seem to update the ACF value in the posts. Here’s my code.

    $query = new WP_Query(
    	array(
    		'post_type'      => 'gyorshirek', 
    		'posts_per_page' => 10, 
    		'post_status'    => 'publish',
    		'meta_key'       => 'ceg_valtozas',
    		'orderby'        => 'meta_value_num',
    		'order'          => 'DESC',
    		
    	)
    );
    
    $elements = $query->posts;
    
    foreach ( $elements as $index => $element ) {
    
    	$custom_fields = get_post_custom( $element->ID );
    	$cpost        = get_post( $custom_fields['ceg_megadasa_gyorshirek'][0] );
    	$cpost_custom = get_post_custom( $cpost->ID );
    	
    
    $ticker = $cpost_custom['feed_id'][0];
    
    $url = 'https://api.portfolio.hu/chart?ticker=' . $ticker . '';
    	$token = 'removed';
    	$args = array(
    		'headers' => array(
    			'Authorization' => 'Bearer ' . $token,
    			'Content-type' =>'application/json; charset=UTF-8',
    		),
    
    		
    	);
    
    	$response = wp_remote_get($url, $args);
    	$body     = wp_remote_retrieve_body( $response );
    	$http_code = wp_remote_retrieve_response_code( $response );
    	$formatted_json = json_decode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    
    	
    
    $cegek_array = array();
    
    // Update ACF FIELDS
    
    	// Save a basic text value.
    	$field_key = "field_60df782a45b20"; // "ceg_valtozas"
    	$value = abs($formatted_json['change_percent']);
    	// echo $value;
    	update_field( $field_key, $value);

    The HTML is rendered after, but I don’t think that is relevant to the question. Any suggestions on what I’m doing wrong?

  • Okay. Clarification. I guess I’m missing something deeper. I could not get this code to work, but after I while, I added this the foreach portion of the code to functions.php (I was running this code before in a template-part, that gets loaded to each page, so I thought that would work as well)

    Adding the code to functions.php made the code work. What was I missing?

  • Edit: I made a mistake, it does not work either way. 😀

  • Before you do the update_field. If you output $value, does it return anything?

    Have you tried adding the post ID to the update_field to see if that works?
    update_field($field_key, $value, $element->ID);

    I assume the closing tag for the foreach isn’t included in the above code?

  • Thanks for getting back to me Jarvis. I managed to circumvent the issue by putting this into a function and invoking it after the template part loads. This way the titles do not appear. Nothing appears which is what I was looking for. I’m unsure what caused the issue but it is fine like this for now. Thank you for the tips!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.