Wikimedia Developer Support

Getting all images from a wikidata item with sparql

How can I get all images for a wikidata item? When I do a sparql query for the property image (P18) I only get one image. Here is my query:

SELECT ?taxon ?taxon_common_name ?taxon_rankLabel ?image WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language “en”. }
?taxon wdt:P31 wd:Q16521;
wdt:P225 “Canis lupus”.
OPTIONAL { }
OPTIONAL { ?taxon wdt:P225 ?taxon_common_name. }
OPTIONAL { ?taxon wdt:P105 ?taxon_rank. }
OPTIONAL { ?taxon wdt:P18 ?image. }
}
LIMIT 1

Remove the LIMIT 1 (query)

Note that this will only give you all the best-rank images – for example, for Ursidae, you would only get the preferred-rank image, not the other normal-rank ones. If you want all the images, you need to use p:P18/ps:P18 instead of wdt:P18 (query).

1 Like