<html lang="it-it" dir="ltr"><head></head><body># Interrogazione dei Prodotti nel Database

 Nel processo di creazione attorno a Easy Digital Downloads potresti trovare la necessità di interrogare il database per dati sui prodotti. Questo documento mostrerà diversi esempi di come farlo correttamente.

### Ottenere un Singolo Prodotto

 Il modo migliore per ottenere informazioni su un singolo prodotto è utilizzare la classe EDD\_Download. Accetta un `download_id` e restituisce un oggetto con informazioni sul download. Esempio:

```
$download = new EDD_Download( 109 );
```

 Questo restituirebbe un oggetto come questo:

```
EDD_Download Object
(
    [ID] =&gt; 109
    [price:EDD_Download:private] =&gt; 
    [prices:EDD_Download:private] =&gt; 
    [files:EDD_Download:private] =&gt; 
    [file_download_limit:EDD_Download:private] =&gt; 
    [type:EDD_Download:private] =&gt; 
    [bundled_downloads:EDD_Download:private] =&gt; 
    [sales:EDD_Download:private] =&gt; 
    [earnings:EDD_Download:private] =&gt; 
    [notes:EDD_Download:private] =&gt; 
    [sku:EDD_Download:private] =&gt; 
    [button_behavior:EDD_Download:private] =&gt; 
    [post_author] =&gt; 2
    [post_date] =&gt; 2016-03-22 13:30:29
    [post_date_gmt] =&gt; 2016-03-22 13:30:29
    [post_content] =&gt; Buy my mp3!
    [post_title] =&gt; My MP3
    [post_excerpt] =&gt; 
    [post_status] =&gt; publish
    [comment_status] =&gt; closed
    [ping_status] =&gt; closed
    [post_password] =&gt; 
    [post_name] =&gt; my-mp3
    [to_ping] =&gt; 
    [pinged] =&gt; 
    [post_modified] =&gt; 2016-03-22 13:30:29
    [post_modified_gmt] =&gt; 2016-03-22 13:30:29
    [post_content_filtered] =&gt; 
    [post_parent] =&gt; 0
    [guid] =&gt; https://edd.dev/?post_type=download&amp;p=109
    [menu_order] =&gt; 0
    [post_mime_type] =&gt; 
    [comment_count] =&gt; 0
    [filter] =&gt; raw
    [post_type] =&gt; download
)
```

 Una volta ottenuto l'oggetto, ci sono una vasta gamma di metodi che possono essere eseguiti su di esso per ottenere informazioni come prezzo, variazioni, ecc. [Documentazione completa per EDD\_Download qui](https://easydigitaldownloads.com/docs/edd_download/).

### Ottenere Prodotti Multipli

 I download sono un tipo di contenuto personalizzato di WordPress, quindi è possibile ottenere gli ID di più download utilizzando `get_posts()`. Se specifichi correttamente il parametro `fields`, `get_posts` otterrà solo gli ID e la query sarà molto più veloce. Dovrai anche specificare almeno `download` per `post_type`. Esempio:

```
    $args = array(
        'fields'    =&gt; 'ids',
        'post_type' =&gt; 'download',
    );

    $downloads = get_posts( $args );
```

 Puoi usare qualsiasi opzione desideri in `get_posts()` per restringere la tua query. [Documentazione completa per `get_posts()` qui](https://codex.wordpress.org/Template_Tags/get_posts).

 Un'opzione aggiuntiva non trovata nella documentazione per `get_posts()` si chiama `no_found_rows()`. Questa rimuove alcuni dati necessari per la paginazione, ma accelera notevolmente la tua query. Se hai bisogno della paginazione, non usare questa opzione. Se non hai bisogno della paginazione, apparirebbe così:

```
$args = array(
	'fields'        =&gt; 'ids',
        'post_type'     =&gt; 'download',
	'no_found_rows' =&gt; true,
);

$downloads = get_posts( $args );
```

 La query sopra restituirà un array di `download_id` come questo:

```
Array
(
    [0] =&gt; 109
    [1] =&gt; 82
    [2] =&gt; 79
    [3] =&gt; 61
    [4] =&gt; 42
)
```

### Lavorare con Prodotti Multipli

 Una volta che hai un array di `download_id`, puoi semplicemente iterare sull'array creando oggetti EDD\_Download come questo:

```
foreach ( $downloads as $key =&gt; $download_id ) {
	$download = new EDD_Download( $download_id );
	// fai quello che desideri con ogni oggetto
}
```</body></html>