I have a Custom Post Type which fetches some data every day for each post and I need to store new values while keeping the old ones. If I use this, it will overwrite previous data:
update_post_meta(get_the_ID(), 'temperature', 25);
update_post_meta(get_the_ID(), 'humidity', 70);
update_post_meta(get_the_ID(), 'ph', 7);
So I was thinking about using something like this to append time to name:
update_post_meta(get_the_ID(), 'temperature-' . time(), 'some_value');
update_post_meta(get_the_ID(), 'humidity-' . time(), 'some_value');
I got all this working, and now I got to the point where I want to retrieve the latest two data for each post - values for yesterday and today. This is where I got stuck.
Is there an easier/Wordpress way for saving the different data every day for every variable inside every CTP post?
When retrieving these variables I was hoping to do it something like this, but maybe I have complicated more than I should have?
$args = array(
'post_type' => 'myctp',
'posts_per_page' => -1 // Retrieve all CTP posts
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) {
$query->the_post();
$post_meta = get_post_meta(get_the_ID());
// here get only 2 latest values for temperature, humidity, ph...
}
endif;