Skip to main contentSkip to footer

Envira Gallery is a gallery plugin for WordPress. Since it uses a custom post type for the galleries, it works fine with Relevanssi. However, the gallery post type doesn’t have any information about the actual images: no titles, tags or other details like that.

Since the images are uploaded in the Media Library and are attached to the gallery posts, this is easy to remedy using the very helpful relevanssi_content_to_index filter hook. Add the following code to your site, set Relevanssi to index the envira post type and rebuild the index:

add_filter( 'relevanssi_content_to_index', 'rlv_envira_gallery_content', 10, 2 );
/**
 * Indexes the attachment content as part of the Envira Gallery posts.
 *
 * @param string  $content The post content.
 * @param WP_Post $post    The post object.
 *
 * @return string The updated content.
 */
function rlv_envira_gallery_content( string $content, WP_Post $post ) : string {
	if ( 'envira' !== $post->post_type ) {
		return $content; // Not a gallery post, skip.
	}
	$args     = array(
		'post_parent'    => $post->ID,
		'posts_per_page' => -1,
	);
	$children = get_children( $args ); // Fetches all the attached images.
	foreach ( $children as $child ) {
		$content .= ' ' . $child->post_title; // Adds the image title.
		$content .= ' ' . $child->post_excerpt; // Adds the image caption.

        $tags = get_the_terms( $child->ID, 'envira-tag' );
		if ( ! is_array( $tags ) ) {
			$tags = array();
		}
		foreach ( $tags as $tag ) {
			$content .= ' ' . $tag->name; // Adds the tag name.
		}
      
		$cats = get_the_terms( $child->ID, 'envira-category' );
		if ( ! is_array( $cats ) ) {
			$cats = array();
		}
		foreach ( $cats as $cat ) {
			$content .= ' ' . $cat->name; // Adds the category name.
		}
	}
	return $content;
}

You can modify the function to include the parts you want included; if you don’t need the categories, just remove that part.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

OceanWP
OceanWP is a popular WordPress theme. It works fine with Relevanssi, but how it handles excerpts on the search results…
Martfury

search. Add this to your site: add_action( ‘wp_loaded’, ‘rlv_replace_martfury_search’ ); /** * Unhooks the Martfury search and replaces it with our custom search. */ function rlv_replace_martfury_search() { global $wp_filter; unset( $wp_filter[‘wc_ajax_martfury_search_products’]->callbacks[10] ); add_filter( ‘wc_ajax_martfury_search_products’, ‘rlv_instance_search_result’ ); } /** * Custom replacement for the dropdown search. Includes all post types in…Martfury is a WooCommerce theme that comes with a built-in dropdown search. That’s great, except that as usual the dropdown search doesn’t work with Relevanssi. Fortunately the search is hooked into place, so it’s possible to hook it out and replace it with something new. That might not be necessary,……you want to include user profiles and taxonomy terms in the search, and not products and other posts, those non-post items won’t appear correctly in the search. The permalinks will be wrong. That’s something you can’t fix with a simple filter hook, and instead you need to replace the whole…

Riode
Riode is a WooCommerce theme from ThemeForest. It has a built-in live search that does not automatically use Relevanssi. However,…

Related Posts:

Comment Section:

5 Comments. Leave new

  • Hello,

    I added this function, but it does not work… Tags added for each picture in Envira Galleries are not included in the search…

    Can you help me?

    Thx. 🙂

    Reply
    • Aurelie, it’s possible Envira has changed something in the product. I’m not familiar with it, so I don’t have a ready solution. I would recommend debugging the code to see why it’s not working. Can it fetch the child images? Does it index everything else but the tags? Is the name of the taxonomy still envira-tag? Do all tags fail to index, or could the problem be explained by stop words? Lots of options there, and the only way to find out is to debug.

      Reply
      • Thank you for your reactive answer! 🙂

        I would like all the tags could be added to the index, but nothing…

        Indeed, I wonder if the problem is not that the name of the taxonomy is still “envira-tag”… I tried with “envira-tags”, but nothing…

        I would like to check what is the real taxonomy used by Envira for tags in data base, but I don’t have access…
        If I find this information, I will give you it in comment. 🙂

        Reply
        • Based on Envira documentation, the taxonomy name is still envira-tag. I don’t think they’d change that lightly. But do examine what the code does at various stages of the function, that’s the best way to find out what’s wrong with it. Go through every step: does it fetch the child posts? Does it get the tags? What happens then? That way you’ll find out where the problem is.

          Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed