Configuring the content and metadata (pmisc) to display in search results

  • khirasaki
    Participant
    3 years, 8 months ago #21355

    I’m trying to figure out where I can configure the p_content and p_misc elements in my search results. Here is my current results page:

    Curated Article Library

    p_misc contains:
    pauthor
    pcat
    pdate

    but I want to attach different metadata (and content) to the results.

    I’m also only seeing “…” for p_content for all of my items; where is that string coming from (or not coming from)? Ideally it would contain a snippet from the article page that contained the matching text. (This may be something I need to configure.)

    wpsolr
    Keymaster
    3 years, 8 months ago #21360

    In WPSOLR screen 2.1, you should select “Use my theme search template”. WPSOLR will then use your standard theme’s search template and url “/?s=”.

    khirasaki
    Participant
    3 years, 8 months ago #21367

    In the special case I’m using it for, I’m not currently using it for my sitewide search template; that’s being handled separately.

    I’m using it in a specific and narrow way to present results from a list of custom-type posts, “Research Articles,” that I have created on the site.

    After some more work, I now have the content area more consistently populating once someone has made a specific search filter query (I will continue hacking at that, as I want to figure out how to surface the list of matching tags in p_content).

    My bigger open question at this point is around how I can pull data from an ACF field and inject it in the search results pmeta. I’ve modified the result.twig file, but I don’t know what function I should be calling from class-wpsolr-abstractsearchclient.php to retrieve ACF fields from the $document. Any quick advice there?

    wpsolr
    Keymaster
    3 years, 8 months ago #21368

    So, you are working on template /wp-content/plugins/wpsolr-pro/wpsolr-templates/twig/search/result.twig

    You can notice a block to display custom HTML:
    {{ result.custom_html }}

    This custom_html data can be set in filter WPSOLR_Events::WPSOLR_FILTER_SOLR_RESULTS_APPEND_CUSTOM_HTML

    You can find an example in the geolocation add-on wp-content/plugins/wpsolr-pro/wpsolr/pro/extensions/geolocation/class-wpsolr-option-geolocation.php:

    		add_filter( WPSOLR_Events::WPSOLR_FILTER_SOLR_RESULTS_APPEND_CUSTOM_HTML, [
    			$this,
    			'wpsolr_filter_solr_results_append_custom_html',
    		], 10, 4 );
    
    	/**
    	 * Generate geolocation distance html to append to results
    	 *
    	 * @param $default_html
    	 * @param $user_id
    	 * @param array $document
    	 * @param WPSOLR_Query $wpsolr_query
    	 *
    	 * @return string
    	 */
    	public function wpsolr_filter_solr_results_append_custom_html( $default_html, $user_id, $document, WPSOLR_Query $wpsolr_query ) {
    
    		$result = '';
    
    		$template_text = WPSOLR_Service_Container::getOption()->get_option_geolocation_result_distance_label();
    
    		if ( ! empty( $template_text ) && $this->get_is_geolocation( $wpsolr_query ) ) {
    
    			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 );
    
    					if ( property_exists( $document, $distance_field_name ) || isset( $document->$distance_field_name ) ) {
    
    						$distance_field_name_translated = WPSOLR_Translate::translate_field_custom_field(
    							WPSOLR_Option::TRANSLATION_DOMAIN_SORT_LABEL,
    							$custom_field_name,
    							WPSOLR_Service_Container::getOption()->get_option_geolocation_user_aggreement_label()
    						);
    
    						$distance = is_array( $document->$distance_field_name ) ? ( $document->$distance_field_name )[0] : $document->$distance_field_name;
    
    						$result .= sprintf( self::TEMPLATE_RESULTS_GEO_DISTANCE,
    							self::WPSOLR_RESULTS_GEO_DISTANCE_CLASS,
    							sprintf( $template_text, $distance_field_name_translated, number_format( $distance, 2, '.', ' ' )
    							)
    						);
    					}
    				}
    			}
    		}
    
    		// No default geolocation default sort, or not a geolocation search: use the general default sort.
    		return $result;
    	}
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.