WordPress.org

Ready to get started?Download WordPress

Forums

Relevanssi - A Better Search
Show custom fields and excerpts (6 posts)

  1. beetrootman
    Member
    Posted 1 year ago #

    Hi, I'm using the excerpt filter to include a custom field "contents". It works fine, but is it possible to only show the custom field in the excerpt if it contains a searched term?

    Also, if the excerpt is pulling some text from the middle of a field (or normal contents) is it possible to have it preceded by three dots, like so: ...some text that contains a term...

    Thanks

    Code I use now:

    add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3);
    function excerpt_function($content, $post, $query) {
    
    $fields = array('custom_contents', 'custom_designerName');
    
    foreach($fields as $key => $field){
    
    $field_value = get_post_meta($post->ID, $field, TRUE);
    $content .= 'Contents:<br />' . ( is_array($field_value) ? implode(' ', $field_value) : $field_value );}
    
    return $content;
    }

    http://wordpress.org/extend/plugins/relevanssi/

  2. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    Well, you have the query in $query, so a simple check to see if $field_value contains $query should do it, right?

    Relevanssi should add three dots in the beginning of an excerpt, yes.

  3. beetrootman
    Member
    Posted 1 year ago #

    Ok, so I have it working as follows. However I noticed a problem (for my needs anyway). When relevanssi is actually making the excerpt from the custom filed or content, if the search term is towards the end of the text, it cuts off the beginning, so that "Contents:" or "Description:" is lost (normal behavior I assume).

    I can only think that it my situation the best solution would be to be able to set three of four different functions for excerpts, therefore I can retain control of how they are used within my template. Would that be possible without modifying the core? I noticed that the filter for this is in excerpt-highlights. It would be possible to add, but I don't really want to mess with that:
    $content = apply_filters('relevanssi_excerpt_content2', $content, $post, $query);

    Current excerpt function:

    add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3);
    function excerpt_function($content, $post, $query) {
    
    if ( 'covers' == get_post_type() ) {
    	if($post->post_content !== "") {
    		$content = "Description: $content<br />";
    	}
    }
    
    $fields = array('custom_contents');
    $search_query = get_search_query();
    foreach($fields as $key => $field){
    	$field_value = get_post_meta($post->ID, $field, TRUE);
    		if (stripos($field_value,$query) !== false) {
    		$content .= 'Contents:' . ( is_array($field_value) ? implode(' ', $field_value) : $field_value );
    	}
    }
    
    return $content;
    }
  4. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    Yes, whatever you add to the exceprt with relevanssi_excerpt_content is just raw material for the usual excerpt-building process.

    The excerpt is filtered through get_the_excerpt after it is built - can you meddle with it at that point?

  5. beetrootman
    Member
    Posted 1 year ago #

    I've tried using get_the_excerpt but I can't figure it out. Even simply trying to add a single word isn't working as I would except. It's adding my text twice. For example with this code

    add_filter('get_the_excerpt', 'filter_excerpt');
    
    function filter_excerpt($descrip) {
    
    return "Test: $descrip";
    }

    I get "Test: Test: ........". Anyway since the complete excerpt is getting passed through the filter, I'm not sure I can do much with it, since I effectively need to have two excerpts to use, one for the normal post content, and one for my custom field. Is it possible to point me in the direction? Thanks

  6. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    Ah, if you're using the_excerpt() to print out the excerpts, get_the_excerpt is being called twice, once by Relevanssi and once by WP.

    Sorry, but I don't really have an answer for you. The best solution I can come up with is making excerpt creation a hooked function, so that you could remove the default excerpt function and replace it with your own without changing the core. Would that make sense to you?

    That is, I'd take large part of relevanssi_do_excerpt() (say, from after the relevanssi_excerpt_content is applied until before the highlighting is applied) out, make it a separate function that is called with a filter hook. Then you could remove that and replace it with your own excerpt function.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic