It is a technical limitation. The meta_key will only be created for every post published after the plugin has been activated.
There is nothing I can do here.
I understand. Thanks.
Internally, I guess there’s an INNER JOIN involved? How can we see the generated query? Not sure if a LEFT JOIN would solve this at once. But I’m considering using a plain MySQL query to retrieve results (on my custom view).
Well, I solved it with something like this:
function set_default_views_metakey($post_ID){
add_post_meta($post_ID, 'views', 0, true);
return $post_ID; // not sure if needed
}
add_action('wp_insert_post','set_default_views_metakey');
It works. New posts now has zero as default views count.
And this will correct the error in existing posts:
$args = array(
//'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'views',
'value' => 'bogus', // you have to pass a value
'compare' => 'NOT EXISTS'
),
array(
'key' => 'views',
'value' => ''
)
),
'fields' => 'ids'
);
// perform the query to get back an array of ids
$posts = new WP_Query( $args );
foreach ( $posts->posts as $id ) {
add_post_meta($id, 'views', 0, true);
}
ref: http://wordpress.stackexchange.com/questions/121165/save-default-value-for-empty-or-missing-post-meta
@gperez-tl: thanks for sharing, this will work if you have a few post, but if your post tables is large, it will probably time out.
But this is a good answer, to be honest I did not thought of this. I am going to pin this topic.
Hi Lester,
Have you updated the plugin with above issue?