Hi,
I have this function:
function numero_rapporto_acf_save_post( $post_id ) {
$numero_rapporto = get_field('numero_rapporto');
$tecnico = get_field('id_tecnico');
$id_tecnico = $tecnico['value'];
$data = date('Y');
if (empty($numero_rapporto)) {
$posts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_key' => 'numero_rapporto',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'id_tecnico',
'value' => $id_tecnico,
'compare' => '='
),
array(
'key' => 'anno_report',
'value' => $data,
'compare' => '='
)
)
));
$recent = $posts[0];
// echo
$number = get_field('numero_rapporto', $recent->ID);
$number2 = $number + 1;
update_field('numero_rapporto', $number2, $post_id);
}
}
add_action('acf/save_post', 'numero_rapporto_acf_save_post', 20);
It works well until 10, then every time I save a post the number is always 10.
Any suggestion?
Thanks in advance
Solved!
Posts were sorted alphabetically.
I changed
'orderby' => 'meta_value',
with
'orderby' => 'meta_value_num',