Skip to main content
Easy Digital Downloads Documentation
Documentation, Reference Materials, and Tutorials for Easy Digital Downloads

Querying Products in the Database

In the process of building around Easy Digital Downloads you may find the need to query the database for product data. This document will show several examples of how to do that properly.

Getting a Single Product

The best way to get information about a single product is to use the EDD_Download class. It accepts a
download_id and returns an object with information about the download. Example:

$download = new EDD_Download( 109 );

This would return an object like this:

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

Once you have the object there are a wide variety of methods that can be run on it to get information like price, variations etc.
Full documentation for EDD_Download here.

Getting Multiple Products

Downloads are a WordPress custom content type, so it’s possible to get the ids of multiple downloads using
get_posts(). If you specify the fields parameter properly then get_posts will get only the ids, and the query will be much faster. You’ll also need to at least specify download for the post_type. Example:

    $args = array(
        'fields'    => 'ids',
        'post_type' => 'download',
    );

    $downloads = get_posts( $args );

You may use any option you wish in
get_posts() to narrow your query.
Full documentation for get_posts() here.

One additional option not found in the documentation for
get_posts() is called no_found_rows(). This removes some data required for pagination, but dramatically speeds up your query. If you need pagination don’t use this option. If you don’t need pagination it would look like this:

$args = array(
	'fields'        => 'ids',
        'post_type'     => 'download',
	'no_found_rows' => true,
);

$downloads = get_posts( $args );

The above query will return an array of download_ids like this:

Array
(
    [0] => 109
    [1] => 82
    [2] => 79
    [3] => 61
    [4] => 42
)

Working With Multiple Products

Once you have an array of download_ids you may simply iterate over the array creating EDD_Download objects like this:

foreach ( $downloads as $key => $download_id ) {
	$download = new EDD_Download( $download_id );
	// do whatever you wish with each object
}
Read our latest blog post:
Add More Stripe Payment Methods at Checkout