I'm trying to perform a scheduled update to all posts, 10 posts at time.
To achieve this, I run Cron job with a script that saves 2 post meta:
$DATA
: the data I want to be update$TIME
: the UNIX timestamp:time()
, to know when that post has been updated with a new$DATA
As I have a lot of posts that do not have the $DATA
postmeta, when I query the posts to perform the update I want the posts to be ordered like this:
- First I want any post that does not have the
$TIME
postmeta - Then all others, starting from the ones I've updated earlier
These are the query arguments I'm using:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '10',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'TIME_meta_key',
'compare' => 'EXISTS'
),
array(
'key' => 'TIME_meta_key',
'compare' => 'NOT EXISTS',
'value' => 'xxx' // I've read this is a bug, so I put a value even if it's ignored
),
),
'orderby' => 'TIME_meta_key',
'order' => 'ASC'
);
As I said before, the TIME_meta_key
value (when exists) is just a UNIX timestamp.
So, as I want to update first the posts that are still not updated, then the ones that have been updated earlier in time, I assume I have to order ASC.
But if I set ASC, this does not work. it keep updating the posts that already have a value in the TIME_meta_key
field.
Is this normal or I'm making some stupid mistake?