Posted on

Envira Gallery

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.

5 comments Envira Gallery

  1. 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. 🙂

    1. 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.

      1. 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. 🙂

        1. 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.

          1. Thank you very much. but I think I will try another plugin, for another usage.

            If I find the solution, of course, I will share it.

Leave a Reply to Mikko Saari Cancel reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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