Skip to content

Instantly share code, notes, and snippets.

@em-piguet
Created May 31, 2018 11:45
Show Gist options
  • Save em-piguet/95894dea25a8f8c975062e0858c7dd88 to your computer and use it in GitHub Desktop.
Save em-piguet/95894dea25a8f8c975062e0858c7dd88 to your computer and use it in GitHub Desktop.
<?php
/**
* This is a muplugin
*
* Indexation des champs ACF "Auteurs" (relationnel) et "Année" (post_date) dans Elasticsearch index
* pour WPSOLR
*/
use wpsolr\core\classes\WPSOLR_Events;
use wpsolr\core\classes\WpSolrSchema;
use wpsolr\core\classes\engines\WPSOLR_AbstractIndexClient; // ne fonctionne pas sans ça
if ( ! class_exists( 'WPSOLR_Events' ) ) {
return;
}
/**
*
* @param array $document_for_update
* @param $solr_indexing_options
* @param $post
* @param $attachment_body
* @param WPSOLR_AbstractIndexClient $search_engine_client
*
* @return array Document updated with fields
*/
function wpsolr_filter_solarium_document_for_update( array $document_for_update, $solr_indexing_options, $post, $attachment_body, WPSOLR_AbstractIndexClient $search_engine_client ) {
$solr_dynamic_type = WpSolrSchema::_SOLR_DYNAMIC_TYPE_STRING; // Depends on the type selected on your field on screen 2.2
$document_for_update[ 'auteur_item' . $solr_dynamic_type] = get_auteur($post->ID);
$document_for_update[ 'an_item_i' ] = get_the_date('Y',$post->ID); // c'est un integer donc on rajoute "_i"
return $document_for_update;
}
add_action( 'after_setup_theme', function () {
add_filter( WPSOLR_Events::WPSOLR_FILTER_SOLARIUM_DOCUMENT_FOR_UPDATE, 'wpsolr_filter_solarium_document_for_update', 10, 5 );
} );
/**
* Output le nom de ou des auteurs attachés au post par un champs ACF relationnel.
*
* @param [int] $post_id
* @return void
*/
function get_auteur($post_id) {
$auteurs = get_field('article_author', $post_id, false);
if ( $auteurs ) :
$auteurs_noms = array();
foreach ( $auteurs as $auteur_id) :
$auteurs_noms[] = get_the_title($auteur_id);
endforeach;
$output = implode(", ", $auteurs_noms);
return $output;
endif;
}
@tazeverywhere
Copy link

you must use full namespace for class_exist() check amigo.
if ( ! class_exists( 'wpsolr\core\classes\WPSOLR_Events' ) ) { return; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment