admin管理员组

文章数量:1289529

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);

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);
Share Improve this question edited Jul 17, 2021 at 16:35 Joy asked May 13, 2021 at 11:46 JoyJoy 317 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

本文标签: pluginsUsing ACF default value to autoincrement a number field