Support

Account

Home Forums General Issues Very slow query

Unread

Very slow query

  • Hi, I am trying to get a field value from the last post but the query is very slow.
    The browser continues working until a message says: Lost connection.
    This is the code:

    add_filter('acf/load_field/name=numero_doc', 'my_acf_default_numero_rapporto');
    function my_acf_default_numero_rapporto($field) {
    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;
    $data = date('Y');
    
    	   if ($current_user_id == 2) { 
           $lettera = 'A';
    	   } else {	   
           $lettera = 'B';
    	   }
    
    if (empty($numero_doc)) {
    
    $args = array(
      'posts_per_page' => 1,
      'post_type' => 'reports',
      'post_status' => 'publish',
      'meta_key'	=> 'numero_doc',   
      'orderby'		=> 'meta_value',
      'order' => 'DESC',
      'meta_query' => array(
    		'relation' 			=> 'AND',
    		array(
    			'key'		=> 'id_tecnico',
    			'value'		=> $lettera,
                'compare' => 'LIKE'
    		),
    		array(
    			'key'		=> 'anno',
    			'value'		=> $data,
                'compare' => '='
    		)
      )
    );
    
    $the_query = new WP_Query( $args ); 
    if ( $the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
    		$numero = get_field('numero_doc'); 
        endwhile; 
    endif; 
    wp_reset_postdata(); 
    }
     $field['default_value'] = $numero + 1;
      return $field;
    }
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.