• I want to search for images in the media library based on their alt-text; accessed via the frond-end with a search-form.
    The results are thumbnails with the alt-text.

    I’ve searched days to find a good plugin or piece of code; but nothing.
    Anyone good advice ?

    I know we can search for _wp_attachment_image_alt (table: wp_postmeta) to find the particular ALT; then we have the post_id to get the image itself.
    I’m not that kind of coder to code that by myself. Any reference or an example of a simular plugin would be a nice start.

Viewing 1 replies (of 1 total)
  • Thread Starter Dave Loodts

    (@davelo)

    Nope, i didn’t found my solution as wanted on the ALT-thing. But i made it to search for the description of the image. Also neat for me.
    So, if you place the same text in the ALT and Description box; that’s solved it.

    here is the code i wanted to share:

    Functions.php

    function ngg_get_search_pictures ($keywords, $numberPicRow = NULL) {
    	global $wpdb;
    	$count=1;
    	if (!$numberPicRow) { $numberPicRow = "4"; }
    
    	$nngquery = "
    				SELECT post_content, post_mime_type, ID, guid
    				FROM wp_posts
    				WHERE post_mime_type = 'image/jpeg'
    				";
    	$pictures = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_mime_type = 'image/jpeg' AND post_content LIKE '$keywords'", ARRAY_A);
    
    	if ($pictures) foreach ($pictures as $pic) { 
    
    		$out .= '<a href="'.$pic['guid'].'" >';
    		$out .=  '<img src="'.$pic['guid'].'" alt="'.$pic['post_content'].'" title="'.$pic['post_content'].'" height="80px"  />';
    		$out .=  "</a>\n";
    				if ($count == 0) {
    				$out .= "<p>&nbsp;</p><br /><br /><br /><br />";
    				}
    				++$count;
    				$count%=$numberPicRow;
    	}
    
    	return $out;
    
    };

    And this in the search.php

    <?php if(is_search()) {
    	$search = $wp_query->get('s');
    	$keywords = preg_replace('/\+/',' ',$search);
    	if (function_exists ('ngg_get_search_pictures')) {  // function from functions.php
    		$nggpictures = ngg_get_search_pictures($keywords, '4'); // put the number of pictures by row you want, if you don't want "4"
    
    		if ($nggpictures) {
    			echo "<h2>Search for pictures</h2>";
    			echo $nggpictures;
    			echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>";
    		}
    	}
    }

    The result is the display of the images with the searched keywords in the description. If you don’t have a ‘screen-filled gallery’ plugin yet; you you can try Easy Fancybox.

Viewing 1 replies (of 1 total)
  • The topic ‘Searching images on ALT-text.’ is closed to new replies.