Skip to content

Instantly share code, notes, and snippets.

@LaxusCroco
Created November 7, 2023 13:36
Show Gist options
  • Save LaxusCroco/38e642ec8ae2e2fcbfc3467998566eba to your computer and use it in GitHub Desktop.
Save LaxusCroco/38e642ec8ae2e2fcbfc3467998566eba to your computer and use it in GitHub Desktop.
Adds the ability to display the name of the related term in the Custom Field area of Ajax Search. In the Key field, enter post_terms__category where the 'category' part should be replaced with the taxonomy slug.
<?php
add_filter( 'jet-search/template/pre-get-meta-field', function($value, $meta, $post){
$meta_key = $meta['meta_key'] ?? '';
if( false === strpos( $meta_key, 'post_terms__' ) ){
return $value;
}
$tax_slug = ( explode('post_terms__', $meta_key) )[1];
if( ! taxonomy_exists( $tax_slug ) ){
return $value;
}
$terms = wp_get_post_terms( $post->ID, $tax_slug, array('fields' => 'names') );
return array( implode(', ', $terms) );
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment