Search Images From Your Next Gen Gallery

I’ve had a few email enquiries recently asking about how I set up the image search function on my next gen wordpress gallery, so I will attempt to answer here.

First off, thanks to ‘Psykotik’ for his helpful tutorial “how to add a search function for next-gen gallery”
Also many thanks to a kind soul called ‘AengusM’ and his very clever girlfriend. Aengus has it working on his site too, he does some great underwater photography Aengus has his search set up to search images only and I have mine to search images and posts. I will try and explain both.

Look for a search.php file in your themes template folder, if you dont have one you can make one by copying the code from index.php and save as ‘search.php’. Open your search.php and look at the code. You are looking for the line


Once you have found it paste in the following code underneath.

	$keywords = preg_replace('/+/',' ',$search);
	if (function_exists ('ngg_get_search_pictures')) {  // function from functions.php
		$nggpictures = ngg_get_search_pictures($keywords, ''); // put the number of pictures by row you want, if you don't want "4"

		if ($nggpictures) {
			echo "


"; echo $nggpictures; } } } ?>

Then open your functions.php file and paste in at the bottom the following code

## Function to do searches on gallery pics from NextGen Gallery plugin
## 2 vars : (1) $keywords (usually coming from the standard search query from wordpress)
## (2) $numberPicCol (number of pic by row, if null it takes 4 )
function ngg_get_search_pictures ($keywords, $numberPicRow = NULL) {
global $wpdb;
if (!$numberPicRow) { $numberPicRow = "4"; }

$nngquery = "
SELECT pid,description,alttext
FROM   wp_ngg_pictures
WHERE  MATCH (description, filename, alttext) AGAINST ('*$keywords*' IN BOOLEAN MODE)
AND exclude = '0'
SELECT pid,description,alttext
FROM wp_ngg_pictures, wp_terms, wp_term_relationships
WHERE pid = term_id = term_taxonomy_id and
MATCH (name) AGAINST ('*$keywords*' IN BOOLEAN MODE)
AND exclude = '0'
$pictures = $wpdb->;get_results($nngquery, ARRAY_A);
if ($pictures) foreach($pictures as $pic) {

$out .= '';
$out .= 'Tutorials  ';
$out .= "n";
if ($count == 0) {
$out .= "";
return $out;

This code should solve some of the following issues that I’ve heard a few people have, like image thumbnails not aligning horizontally and resulting search images not hyperlinked.

If you would like to display just pictures and no posts then simply remove from your search.php

Although this system works it is by no means perfect and you may get some wayward results. However I believe that a more intergrated solution with next-gen is not far away. Check wordpress forum for updates.

UPDATE: Their is now a plugin from Alex Rabe that searches images From Your Next Gen Gallery , so please see

You may also like