• I am using the relevanssi_excerpt_content filter and the search function works great. One issue I noticed somewhat recently is that the custom excerpts generated for search results are showing outdated content. For instance, I have removed a specific term from a page, but when I search for that term the page still shows in the results along with the old content as it appeared before I changed it.

    I have cleared cache and rebuilt the index and that did not work. Any other thoughts on what may be causing this?

    • This topic was modified 6 years, 1 month ago by jbarlowga.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mikko Saari

    (@msaari)

    What kind of content are you talking about here? Just plain post content, custom fields, shortcode content or something else?

    When Relevanssi builds excerpts, it reads the post content from the posts database directly, so it should not be possible to get outdated content at that time.

    Thread Starter jbarlowga

    (@jbarlowga)

    I am using Advanced Custom Fields. The excerpts that are showing in search results contain content that is no longer on the respective pages.

    I might add that the content I am having issues with is in ACF accordion repeater fields. I don’t know if other fields are impacted as well, but in the particular case I am referring to the old excerpts are coming from these fields. Not sure if that helps or not.

    • This reply was modified 6 years, 1 month ago by jbarlowga.
    Plugin Author Mikko Saari

    (@msaari)

    Relevanssi takes the ACF content directly from the custom fields. Relevanssi doesn’t know what is displayed on the page. If the content exists in the custom fields, it may be used for the excerpt. Is this the cause here? Is the content you’re seeing in the excerpt still somewhere in the custom fields?

    Thread Starter jbarlowga

    (@jbarlowga)

    The content only appears in previous versions. It does not appear anywhere on the current page. This is why I don’t understand where the excerpt is coming from. It’s like Relevanssi is looking at version history and pulling content from there. However, it also appears to only be the accordion repeater field that is having this issue.

    Plugin Author Mikko Saari

    (@msaari)

    Since the free version of Relevanssi doesn’t handle repeater fields, are you using some special code to fetch the repeater field content? Or have you just set Relevanssi to use all custom fields or all visible custom fields?

    Relevanssi doesn’t look at revisions: Relevanssi uses the current post ID and get_post_meta() to get the custom field content, so it shouldn’t be possible that Relevanssi fetches the content from old revisions.

    You can use the relevanssi_custom_field_value filter hook to filter the custom field content Relevanssi sees and will use for excerpts. You could for example try this:

    add_filter( 'relevanssi_custom_field_value', 'rlv_test_fields', 10, 3 );
    function rlv_test_fields( $value, $field, $post_id ) {
        echo "Field: $field from post $post_id has value '$value'<br >";
        return $value;
    }

    Add this to your theme functions.php, then search for an offending post (as strict search as possible), and see what it prints out. It will show you exactly what Relevanssi sees from the fields.

    Thread Starter jbarlowga

    (@jbarlowga)

    I am using the premium version that our vendor set up. A developer wrote the code before I was hired, so I am not entirely sure how it all works. The code used is below. I hope this helps a little more. All I know is that the excerpt that is showing for search results contains content from old page versions.

       add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3); 
        function excerpt_function($content, $post, $query) {
    
            global $wpdb; $fields = $wpdb->get_col("SELECT DISTINCT(meta_key) FROM $wpdb->postmeta");
    
            // ignore these fields
            $ignore = array('_edit_lock', '_edit_last', '_wp_page_template');
            foreach($fields as $key => $field){ 
                if(!in_array($field, $ignore)){
                    $field_value = get_post_meta($post->ID, $field, TRUE);
                    if(!ctype_digit($field_value)) $content .= ' ' . ( is_array($field_value) ? implode(' ', $field_value) : $field_value );
                }
            }
            // Remove random terms from showing in the search. These are related to the names of the ACF field names
            $wordlist = array('acf_id_1', 'acf_id_2', 'acf_id_3', 'acf_id_4');
            foreach ($wordlist as &$word) {
                $word = '/\b' . preg_quote($word, '/') . '\b/';
            }
            $wordlist = array('/field_\w*/', '/\w*_callout/', '/\w*_content/', '/banner_image/', '/banner_image_with_overlay/', '/small_gray_tile_links/', '/large_gray_tile_blocks/', '/gray_tile_blocks/', '/profile_blocks/', '/block_links/', '/three_columns_and_image/', '/leadership_profiles/', '/gray_tile_blocks/', '/vertical_slider/', '/tile_layout/',
            	'/two_columns_70-30/', '/two_columns_50-50/', '/two_columns_with_small_accordions/', '/content_with_2_column_text/', '/page_header/', '/content_header/', '/content content_subheader/', '/content small_accordions/', '/content sidebar/', '/content accordions/',
            	'/content two_columns accordions/', '/\[download_tools\]/', '/\[related_info\]/', '/two_columns/', '/sidebar/', '/content/', '/\#bb0000/', '/notify/', '/onehr_category/');
            $content = preg_replace($wordlist, '', $content);
    
            // The excerpt ready with bits removed from it
            return $content;
        }
    

    and it is being called on search.php with

    <?php
    if ( have_posts() ) { 
    	while ( have_posts() ) { 
    		the_post();
    
    		<div class="content-article search-entry clearfix">
    			<a class="content-article-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    			<div class="content-article-text"><?php the_excerpt(); ?></div>
    		</div>
    	
    <?php
    	}
    }
    else { 
    ?>
    	<div class="search-no-results"><p>No results were found.</p></div>
    <?php
    }
    ?>
    Plugin Author Mikko Saari

    (@msaari)

    If you have a Premium license, you should be at the Premium support.

    In any case, that is not well built, it’s adding all sorts of garbage fields to the excerpts.

    For starters, I’d disable that function, make sure the custom field indexing setting is set to only index visible fields and not all fields, because indexing all fields with ACF is going to get you a ton of garbage in the index.

    Then enable “Use custom fields for excerpts” on the excerpts and highlights settings tab in Relevanssi settings.

    I’d start with that. Then if you still have problematic content showing up in the excerpts, you can clean that.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Search results showing old content in excerpts’ is closed to new replies.