That depends. Do the images exist as individual posts in the wp_posts
database? Are they in the media library? I don’t know how Envira Galleries works, so I can’t say for sure, but if the images exist as individual posts in the media library, then there should be no problems in having the search find them: just index the attachment
post type and include that in the search.
Thread Starter
MiKemp
(@mikemp)
Thanks.
Attachment post type is indexed, and the search does find them.
But the search results show (and link to) the *page* on which that image/caption can be found.
I am hoping for a way for the search results to show (and link to) the specific image.
When the search finds an attachment page, the default setting is to point to the attachment page (not the post or page that has the attachment, but the WP-generated page for that single image). That can be modified in the search results template so that the search result points directly to the image.
Currently, searching for the attachment post type on your page (by adding &post_types=attachment
at the end of the search page URL) finds nothing, so to me it seems like the attachments aren’t even indexed. Can you show me a search that does find a post of attachment
post type?
Thread Starter
MiKemp
(@mikemp)
I’m afraid I have wasted your time… looking deeper, when I compare an image that appears in an Envira gallery with the same image in the Media library, I find that the caption is in the Envira gallery but not in the Media library, and when I find that same image in the DB, post_content is empty. I think this means the caption is not stored in the DB and is therefore not searchable.
Envira probably stores the caption somewhere in its own files. In that case it’s probably possible to index it, but it will be more complicated: you’d have to write some code that finds the image in the Envira database and gets the caption from there. But that gets a bit complicated.
Thread Starter
MiKemp
(@mikemp)
Thanks, Mikko. Just heard back from Envira. They did say, “…when captions are added to images in Envira, they’re not saved for the image in the WP Media Library, and vise-versa.”
Envira stores the image title in the gallery post meta data. Here’s a function that goes through all gallery post meta data and digs the image titles, alt texts and captions from there:
add_filter( 'relevanssi_content_to_index', 'rlv_envira_metadata', 10, 2 );
function rlv_envira_metadata( $content, $post ) {
global $wpdb;
$gallery_data = $wpdb->get_col(
$wpdb->prepare(
"SELECT meta_value FROM $wpdb->postmeta AS pm, $wpdb->posts AS p WHERE p.ID = pm.post_id AND p.post_type = %s AND meta_key = %s",
"envira", "_eg_gallery_data"
)
);
foreach ( $gallery_data as $data ) {
$gallery = unserialize( $data );
if ( isset( $gallery['gallery'][ $post->ID ] ) ) {
$photo_content[] = $gallery['gallery'][ $post->ID ]['title'];
$photo_content[] = $gallery['gallery'][ $post->ID ]['alt'];
$photo_content[] = $gallery['gallery'][ $post->ID ]['caption'];
$content .= ' ' . implode( ' ', $photo_content );
}
}
return $content;
}
If you add this to your theme functions.php
and make Relevanssi index the attachment
post type, you should be able to find posts by their titles and captions.
Thread Starter
MiKemp
(@mikemp)
Wow, thank you. I have added the code to functions.php.
Now when searching on a name that is in a caption in an Envira Gallery, the search returns:
1. As before, it shows the page on which that image exists. What is shown is the name of the page, and the featured image, nothing specific to the image. Click takes you to the page.
2. It also returns the *title* of the image that is in the *Media Library* (e.g. 052628_0037). What is shown is the title, nothing else. Click on the title, get image as is in ML.
Those images in the Media Library do not have captions (or correct titles). The title and caption that are displayed on the front end are in the Envira gallery.
I’m afraid I’m not explaining this well, so have put up an image that shows the diff between the media library item and the EG item. Please see https://cl.ly/f429dea86f04
If it’s possible… find and display the Envira Gallery title, not the Media Library title? With the result that a click would show the item as it is in the EG?
By the way, I greatly appreciate you going so far beyond “free” support. If you think you can make this work, I would go to the site owner and tell him it can work, and will cost X dollars, and see what he says. Unless you’re just having too much fun… 🙂
Yes, I know there’s a difference between Envira Gallery and the Media Library. That’s tricky, especially if the same image appears in multiple galleries with different captions; if the images appear only in one gallery each, getting reasonable search result pages is possible – a similar method can be used to dig in the titles and captions for excerpts.
Adding this should help a little with the excerpts:
add_filter( 'relevanssi_excerpt_content', 'rlv_envira_metadata_excerpts', 10, 2 );
function rlv_envira_metadata_excerpts( $content, $post ) {
global $wpdb;
$gallery_data = $wpdb->get_col(
$wpdb->prepare(
"SELECT meta_value FROM $wpdb->postmeta AS pm, $wpdb->posts AS p WHERE p.ID = pm.post_id AND p.post_type = %s AND meta_key = %s",
"envira", "_eg_gallery_data"
)
);
foreach ( $gallery_data as $data ) {
var_dump($data);
$gallery = unserialize( $data );
if ( isset( $gallery['gallery'][ $post->ID ] ) ) {
$photo_content[] = $gallery['gallery'][ $post->ID ]['caption'];
$content .= ' ' . implode( ' ', $photo_content );
}
}
return $content;
}
This will make Relevanssi use the captions for excerpts. To get the title showing is a bit more complicated and requires changing your search results templates – another approach is to just give the photos good titles in the Media Library. Or you could create a plugin that copies the Envira captions and titles to Media Library, that too would sort this out.
Thread Starter
MiKemp
(@mikemp)
Okay, thank you again for such outstanding support.