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

 <?php if (have_posts()) : ?>,

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

get('s');
	$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 "

<h2>Pictures</h2>
";
			echo $nggpictures;
		}
	}
}
?&gt;

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;
$count=1;
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'
UNION
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 .= '<img class="ngg-gallery" style="border: .1em solid #FFFFFF" title="'.stripslashes($pic[alttext])
.'" src="'.nggGallery::get_thumbnail_url($pic[pid]).'" alt="'.stripslashes($pic[alttext]).'" />';
$out .= "\n";
if ($count == 0) {
$out .= "";
}
++$count;
$count%=$numberPicRow;
}
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 http://wordpress.org/extend/plugins/nextgen-gallery-search/

Be Sociable, Share!
This entry was posted in Tutorials and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

15 Comments

  1. Posted June 10, 2009 at 5:11 pm | Permalink

    line ‘$pictures = $wpdb->get_results($nngquery, ARRAY_A);’ crashed my system. Changing > to > solved it.

  2. Posted June 10, 2009 at 5:14 pm | Permalink

    line

  3. Posted July 27, 2009 at 12:02 am | Permalink

    hey Ranno,

    Thanks for that for that info, error must have got there when I was pasting the code into the post. Problem now fixed.

    Best wishes,

    tom

  4. Eric
    Posted August 30, 2009 at 9:36 pm | Permalink

    Thank you for this but I am having trouble getting it to work:
    Followed the instructions per your post.

    Pasting that last block of code at the bottom functions.php as described broke WP so I pasted it just above:

    Still does not seem to work. Any additional thoughts?

  5. Eric
    Posted August 30, 2009 at 9:38 pm | Permalink

    …just above the last PHP tag and the last 3 closing DIV tags (sorry my lost post did not show the tags).

  6. Posted December 8, 2009 at 10:23 pm | Permalink

    Previous pastes did not work with my Nextgen

    I have created this (copy pasted some different techniques together):

    In the functions:

    /**
    ** Function to do searchs on gallery pics from NextGen Gallery plugin
    **
    ** @keywords keywords, usually gave by the standard search query from wordpress
    ** @numberPicCol number of pic by row. If null, it takes 4 by default
    */
    function ngg_get_search_pictures ($keywords, $numberPicRow = NULL) {
    global $wpdb;
    $count=1;
    if (!$numberPicRow) { $numberPicRow = "4"; }

    $nngquery = $wpdb->prepare( "
    SELECT pid,description,alttext
    FROM " .$wpdb->prefix. "ngg_pictures
    WHERE MATCH (description, filename, alttext) AGAINST (%s IN BOOLEAN MODE)
    AND exclude != 1"
    , "*".$keywords."*");
    $pictures = $wpdb->get_results($nngquery, ARRAY_A);

    if ($pictures) foreach($pictures as $pic) {

    $out .= '';
    $out .= '';
    $out .= "
    \n";

    if ($count == 0) {
    $out .= "";
    }

    ++$count;
    $count%=$numberPicRow;
    }
    return $out;
    };

    In de search.PHP

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

    echo "Gevonden afbeeldingen voor zoekwoord(en)";
    echo "\"" ;
    echo $search ;
    echo "\"";
    if ($nggpictures) {
    echo $nggpictures;
    echo ' ';
    }
    else {
    echo 'Geen afbeeldingen gevonden voor uw zoekopdracht.';
    }
    }
    }
    // End of NextGen Gallery search
    ?>

  7. Posted January 14, 2010 at 9:43 pm | Permalink

    The more integrated solution has indeed happened. Yesterday (jan. 13 2010) Alex Rabe introduced a plugin for nextgen search. The result is GREAT! It seems to be fully integrated with your nextgen gallery so that all of your settings will apply. One caveat… it still doesn’t seem to search for tags. If only I knew how to code…

  8. Posted March 26, 2010 at 11:06 am | Permalink

    I searched and searched and I finaly got some progress… I just need help with the last part.
    Ill start with what I’ve done and tried alrdy.

    http://www.ikiru.ch/blog/2008/how-to-add-a-search-function-for-nextgen-gallery-wordpress-plugin
    (I am cerafil in the comments)
    Also Check out my threads started about it, perhaps it will give more info

    http://wordpress.org/support/topic/358242?replies=3
    and
    http://wordpress.org/support/topic/378238?replies=2

    But Then finaly today…
    * I finaly integrated a search.php (because mine had only searchform.php and not the “lines” I needed.
    * I tried to update my functions.php .. it crashed a few times but I finaly got 2 different results.
    At Start> I saw this cod in the bottom of my site rather then it to show up on top with the images requested.

    Pictures
    “; echo $nggpictures; } } } ?>

    * I tried putting “bas smids” code to echo but then I got the entire code showing “above” the Pictures.

    get(‘s’); $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 ”
    Pictures
    “; echo $nggpictures; } } } ?>

    thanks for looking into it.. pls mail me for questions if you need more info.
    I put the old functions.php back now.

  9. Posted March 26, 2010 at 11:11 am | Permalink

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

    in the very bottom of the code? :p

  10. RRG
    Posted May 13, 2010 at 8:31 pm | Permalink

    Don’t work. I can follow directions, still nothing. I wish it did, its a feature I really want.

  11. Posted May 13, 2010 at 8:53 pm | Permalink

    There have been many updates to both wordpress and next gen gallery since first making this solution. It is not something I continue to support since the release of a plugin that does the job.

    Please see http://wordpress.org/extend/plugins/nextgen-gallery-search/

  12. Posted May 18, 2010 at 8:32 pm | Permalink

    Tom,
    You must have not have actually read or tried that plug-in as it is a admin tool, not a front end, end user tool.

    RRG

  13. Posted May 18, 2010 at 9:05 pm | Permalink

    whoops – thanks RRG for pointing out my error – I linked to the wrong plugin – I have amended the link now – please try the plugin here http://alexrabe.de/2010/01/13/search-for-images/ let me know if this helps, if not I will help you to get the above code updated. I am still not sure what your problem is and would need some more info to help.

  14. Posted May 18, 2010 at 10:58 pm | Permalink

    Tom,
    I actually came back here after finding the plug-in to post it for you. But thanks anyway! I did install it and It works (sort of). Its a complex reason (using nextgen in a shopping cart app to view images) but I’m now on the hunt to sort out a way to have it only look in the tags rather than in tags, file name and description. I have duplicate products in multiple galleries so when you do a search for “white” all those duplicates with the name “white” come up when searching. I could avoid this if it used Tags and only put in “white” on one product. I’m no php expert, but I know code. Trying to find it now where it searches for them, I’m sure its a “global” term or something and needs to be replaced with something specific. (I have no idea what)

    If you have any ideas, please send them my way. thanks for the link.
    RRG

  15. Posted August 16, 2010 at 9:33 pm | Permalink

    hi, well all is working great but i want the search to look for only exact expretions, is that possible ???

    allsow it is looking for a 4 charts minimum, i want the minimum to be 3 charts, is this possible ??

    i will be verry greatfaul for any kind of help…

    thank you and sorry for the bnd spelling

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>