Fatal Error: WPSOLR_Events not found
- wpsolrKeymaster5 years, 3 months ago #12606
You need to wait until WPSOLR is loaded.
See https://www.wpsolr.com/guide/actions-and-filters/:
Your declaration of the hook containing a reference to the php class WPSOLR_Events, it must be executed after the WPSOLR plugin is loaded.
For instance, in:
use wpsolr\core\classes\WPSOLR_Events;
add_action( ‘after_setup_theme’, function () {
… declare your WPSOLR actions or filters here
} );or in
use wpsolr\core\classes\WPSOLR_Events;
add_action( ‘admin_init’, function () {
… declare your WPSOLR actions or filters here
} );wpsolrKeymaster5 years, 3 months ago #12620Here is an example taken from /wp-content/plugins/wpsolr-pro/wpsolr/pro/extensions/geolocation/class-wpsolr-option-geolocation.php
/** * Add distance custom fields to the post results * * @param WPSOLR_Query $wpsolr_query * @param WPSOLR_AbstractResultsClient $results */ public function wpsolr_action_posts_results( WPSOLR_Query $wpsolr_query, WPSOLR_AbstractResultsClient $results ) { if ( empty( $wpsolr_query->posts ) || empty( $results ) ) { // No results: nothing to do. return; } // Name of the field added to the post containing a list of distances $field_distance_name = WPSOLR_Regexp::remove_string_at_the_end( self::GEOLOCATION_DISTANCE_FIELD_PREFIX, '_' ); foreach ( WPSOLR_Service_Container::getOption()->get_option_index_custom_fields( true ) as $custom_field_name ) { if ( self::_SOLR_DYNAMIC_TYPE_LATITUDE_LONGITUDE === WpSolrSchema::get_custom_field_solr_type( $custom_field_name ) ) { // Add geolocation fields to the fields $distance_field_name = $this->get_distance_field_name( $custom_field_name ); foreach ( $results->get_results() as $document ) { if ( $document->$distance_field_name ) { foreach ( $wpsolr_query->posts as $post ) { if ( $post->ID === (int) $document->PID ) { if ( empty( $post->$field_distance_name ) ) { $post->$field_distance_name = []; } $distance = is_array( $document->$distance_field_name ) ? ( $document->$distance_field_name )[0] : $document->$distance_field_name; array_push( $post->$field_distance_name, (object) [ 'field_name' => $custom_field_name, 'distance' => number_format( $distance, 2, '.', ' ' ), // distance formatted 'distance_number' => $distance, // distance not formatted ] ); } } } } } } return; }
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic.