Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created March 1, 2021 18:41
Show Gist options
  • Save jchristopher/24e869fd87f60a22bfe8aa2850d6ecce to your computer and use it in GitHub Desktop.
Save jchristopher/24e869fd87f60a22bfe8aa2850d6ecce to your computer and use it in GitHub Desktop.
Tell SearchWP to index Gmedia (Grand Media) Tags alongside Gmedia Albums
<?php
// Add a SearchWP custom Custom Field to append Gmedia Media Tags to Gmedia Albums when
// indexing, making Gmedia Tags searchable, with Albums coming up for search results.
// @link https://wordpress.org/plugins/grand-media/
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
global $gmDB, $wpdb;
if ( 'post' . SEARCHWP_SEPARATOR . 'gmedia_album' !== $entry->get_source()->get_name() ) {
return $data;
}
// The $entry is the WP_Post, but we need the Gmedia term ID.
$album_id = get_post_meta( $entry->get_id(), '_gmedia_term_ID', true );
$images = $gmDB->get_gmedias( [ 'album__in' => [ $album_id ], 'fields' => 'ids', ] );
$tags = $gmDB->get_gmedia_terms( $images, [ 'gmedia_tag' ], [ 'fields' => 'names' ] );
if ( ! empty( $tags ) ) {
$data['meta']['gmedia_tags'] = implode( ' ', $tags );
}
return $data;
}, 20, 2 );
add_filter( 'searchwp\source\attribute\options\special', function( $keys, $args ) {
if (
$args['source'] !== 'post' . SEARCHWP_SEPARATOR . 'gmedia_album'
|| $args['attribute'] !== 'meta'
) {
return $keys;
}
if ( ! in_array( 'gmedia_tags',
array_map( function( $option ) { return $option->get_value(); }, $keys )
) ) {
$keys[] = new \SearchWP\Option( 'gmedia_tags', 'Gmedia Tags' );
}
return $keys;
}, 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment