0

I am trying to create a field called "numero" as an auto increment field with advanced custom fields.

This function with WP_Query returns the last inserted "numero".

function get_last_numero(){
$args = array(
        'posts_per_page'=> 1,
        'post_type'     => 'sep_doc',
        'post_status' => 'publish, draft, trash',
        'meta_key'      => 'numero',
        'orderby' => 'meta_value_num',
        'order'         => 'DESC'
    );
$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $pid = get_the_ID();
        $numero = get_field(numero,$pid);
    }
}
wp_reset_postdata();
return (int)$numero;

} And this sets the default value in ACF "numero" field as whatever I put in the $numero_id variable;

$numero_id =  get_last_numero() + 1; // tested with static value 123456 - IT's WORKING

add_filter('acf/load_field/name=numero', function($field) use ($numero_id) {
   $field['default_value'] = $numero_id;
   return $field;
});

Static values and all other functions like get_the_ID() works just fine. But when I put get_last_numero() it crashes the site with error 500.

I believe the reason as I am somehow calling "numero" inside "numero", Can anyone suggest any solutions or workaround to this.

Edit: used a solution like this-

var num = <?php echo get_last_numero(); ?>;
$("#acf-post-field_606b65932d135").val(num+1).prop('readonly',true);

1 Answer 1

2

Not a direct answer, but two suggestions.

First of all, get_last_numero does not necessarily return the highest number. It returns the last one. If you have full control over insertion then it will be no problem, but once you will do an import, or change things in the database manually or in other ways than the usual, you might introduce problems. So, in your get_last_numero function, you might want to sort on that (meta) query value DESC. ACF has a function for meta queries to sort numerically.

Second, get_last_numero() + 1 might seem to work, but you're adding a string and a number, which is not a good thing to do. PHP will figure it out in most cases, but since ACF will return a string for a field and you know that, you could cast your return value to integer by preceding the value with (int), so return (int)$numero

5
  • The string + integer conflict is one I kept hitting over and over until it just became routine, I suspect Jos Faber is 100% spot on here. Commented May 13, 2021 at 16:52
  • Yes, I changed the function with 'meta_key' => 'numero', 'orderby' => 'meta_value_num', 'order' => 'DESC' and added the (int) as well. It didn't work though. But finally used javascript to put that value in the input field and made that field readonly.
    – Joy
    Commented Jul 17, 2021 at 16:32
  • So this was working fine, until I decided to rearrange the posts by meta_field_date order by modifying pre_get_posts . Now if date is ordered, its messing the numero. What will be the best way to find the last / highest of the numero field? I am using different code snippets.
    – Joy
    Commented Sep 15, 2021 at 7:38
  • Sounds like the pre_get_posts action will change the sorting and thus you won't necessarily get the highest numero anymore. So in get_last_numero you should disable that filter / action right before the query.
    – Jos
    Commented Sep 16, 2021 at 8:48
  • Or, maybe better, you could cook up a native SQL query to get the highest numero. Here's an example that might work: SELECT m.meta_value FROM wp_posts p LEFT JOIN wp_postmeta m ON m.post_id = p.ID WHERE p.post_type = 'sep_doc' AND m.meta_key = 'numero' GROUP BY p.ID ORDER BY (m.meta_value * 1) DESC LIMIT 1 Mind you: this will bypass all optimizations Wordpress does on database traffic. You might want to store / cache that value until the sep_doc list is updated and than recache that.
    – Jos
    Commented Sep 16, 2021 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.