I have the following code on the website:
$args = array(
'posts_per_page' => -1,
'post_type' => 'gyorshirek',
);
$loop = new WP_Query( $args );
$wp_query = $loop;
if ( $loop->have_posts() ):
while ( $loop->have_posts() ): $loop->the_post();
$custom_fields = get_post_custom( $loop->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 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJUxZF6c2RlZsOzcnVtIGFkYXRzem9sZ8OhbHRhdMOhcyIsImlhdCI6MTUxNjIzOTAyMiwiaXNzIjoicG9ydGZvbGlvLmh1IiwiZXhwIjoxNjcyNTI3NTk5LCJzdWJzIjp7Im1pIjoiMVkifX0.LPeFFyLUJmIw2xbRPcJjHhNaGQE6daI4NUaMJtL76PU';
$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);
if (isset($formatted_json['change_percent'])) {
$change_percent = $formatted_json['change_percent'];
} else {
$change_percent = 0;
}
if (isset($formatted_json['ticker'])) {
$stockticker = $formatted_json['ticker'];
}
if (isset($formatted_json['last'])) {
$lastprice = $formatted_json['last'];
}
$field_key = "field_60df782a45b20"; // "ceg_valtozas"
$value = $change_percent;
update_field( $field_key, $value);
wp_reset_postdata();
endwhile;
endif;
I only want this code to be used to update a field of a custom post type.
The code works, however it breaks my front page by outputting the title of each post.
I can’t figure out what is causing this inside this loop. If I delete it, the problem goes away.
Any ideas?