Fatal Error: WPSOLR_Events not found

  • ENMUROS
    Participant
    # 3 weeks, 6 days ago

    I am getting a fatal error when trying to use WPSOLR_Events::WPSOLR_ACTION_POSTS_RESULTS:

    Fatal error: Uncaught Error: Class ‘WPSOLR_Events’ not found

    How do I reference and include the Events class?

    wpsolr
    Keymaster
    # 3 weeks, 6 days ago

    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
    } );

    ENMUROS
    Participant
    # 3 weeks, 5 days ago

    Ok, so the syntax should look something like this?

    add_action( ‘after_setup_theme’, function () {
    add_action(WPSOLR_Events::WPSOLR_ACTION_POSTS_RESULTS, ‘wpsolr_action_results’, 10, 1);
    }

    wpsolr
    Keymaster
    # 3 weeks, 5 days ago

    Looks like that, yes.

    ENMUROS
    Participant
    # 3 weeks, 5 days ago

    How would I modify the post results? I would like to replace a string in the result set.

    wpsolr
    Keymaster
    # 3 weeks, 5 days ago

    Here 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;
    }
    ENMUROS
    Participant
    # 3 weeks, 5 days ago

    Thank you. How would I pass $wpsolr_query and $results? Could you add an example add_action passing those variables?

    wpsolr
    Keymaster
    # 3 weeks, 5 days ago

    You have nothing to do. The parameters are passed by WordPress.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.