• In my functions.php I have the following code which adds some external content from our newsletters to the search index:

    
    add_filter('relevanssi_content_to_index', 'add_extra_content', 10, 2);
    add_filter('relevanssi_excerpt_content', 'add_extra_content', 10, 2);
    function add_extra_content($content, $post) {
       if( $post->post_type === 'newsletters' ) {
         $glink = get_post_meta($post->ID, 'html-attachment', true);		
         if( ! empty( $glink ) ) {
    	$txt = file_get_contents( 'http://jchwebdev.com/newsletters/' . $glink);
    	if( ! empty($txt) )
              $content .= $txt;
    	}
       }
       return $content;
    }
    

    For some reason, this is causing the search results to be incorrect for (apparently) a single page. What happens is that if I type in the search term ‘jchwebdev’ the first result is the home page (which makes sense) but the EXCERPT contains text pulled from one of the external newsletters–which makes no sense since they aren’t even the same post type. And this excerpt is not random; it’s the same every time. I’ve rebuilt the index and I get the same excerpt so what is going on with the filter that it’s pulling the wrong excerpt?

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

    (@msaari)

    Well, have you checked what the function does when it’s run on the home page? What’s in the $content when the function starts, and what’s in the $content when the function is done?

    Thread Starter suntower

    (@suntower)

    Hi Mikko,

    Not sure I understand what you mean. The function only seems to run when the index is built.

    I added this bit of code to test for the page in question:

    if( $post->ID == 1294 ) {	
        dpm($post);
        dpm($content);
      }

    When I examine the $post parameter it looks as expected (see below). The $content parameter is an empty string of len 0.

    Any ideas?

    object(WP_Post)#927 (25) {
      ["ID"]=>
      int(1294)
      ["post_author"]=>
      string(1) "1"
      ["post_date"]=>
      string(19) "2016-10-20 15:04:57"
      ["post_date_gmt"]=>
      string(19) "2016-10-20 22:04:57"
      ["post_content"]=>
      string(11582) "blah, blah, blah..."
    ["post_title"]=>
      string(9) "JCHWebDev"
      ["post_excerpt"]=>
      string(154) "Marketing, Design and Web Development in Seattle by JC Harris. Reliable service. Beautiful work. Full-service agency experience at a price you can afford!"
      ["post_status"]=>
      string(7) "publish"
      ["comment_status"]=>
      string(6) "closed"
      ["ping_status"]=>
      string(6) "closed"
      ["post_password"]=>
      string(0) ""
      ["post_name"]=>
      string(9) "jchwebdev"
      ["to_ping"]=>
      string(0) ""
      ["pinged"]=>
      string(0) ""
      ["post_modified"]=>
      string(19) "2016-10-27 12:59:39"
      ["post_modified_gmt"]=>
      string(19) "2016-10-27 19:59:39"
      ["post_content_filtered"]=>
      string(0) ""
      ["post_parent"]=>
      int(0)
      ["guid"]=>
      string(34) "http://jchwebdev.com/?page_id=1294"
      ["menu_order"]=>
      int(0)
      ["post_type"]=>
      string(4) "page"
      ["post_mime_type"]=>
      string(0) ""
      ["comment_count"]=>
      string(1) "0"
      ["filter"]=>
      string(3) "raw"
      ["indexing_content"]=>
      bool(true)
    }
    • This reply was modified 7 years, 5 months ago by suntower.
    Plugin Author Mikko Saari

    (@msaari)

    Ok, so $content is empty. That’s what it should be.

    If the function doesn’t run at all when a search is made, where do the excerpts come, then?

    Hard to say, I’d need to be able to debug this a bit to be able to say anything more specific.

    Thread Starter suntower

    (@suntower)

    I’m confused by your statement, “If the function doesn’t run at all when a search is made, where do the excerpts come, then? ”

    relevanssi_content_to_index() runs when index is built, right?

    And relevanssi_excerpt_content() runs during a search, right?

    So I would expect my little filter function to return whatever is in your index (wherever -that- is). Is there some row/column data I can give you from the WP tables to help you troubleshoot? Any -other- info I can provide?

    FWIW: My original assumption was that this results is garbage from some bad pointer (sorry, old guy alert) but I can tell you that it is completely repeatable. ie. I have -removed- Relevannsi, deleted your tables and then re-installed and the exact same garbage result is returned for the same page.

    Please let me know how I can help. It’s kinda driving me nuts. Thanks.

    Plugin Author Mikko Saari

    (@msaari)

    When you said “The function only seems to run when the index is built.”, I assumed you meant it doesn’t run when search is done. It most certainly should do that.

    But if the function returns an empty string when it runs when searching, then I’m not sure where the excerpt is coming.

    Can you show me your search results template?

    Thread Starter suntower

    (@suntower)

    Below is my search.php

    What I meant in my previous msg is that I thought that relevanssi_excerpt_content() runs for every search, but relevanssi_content_to_index() only runs when the index is built. Do I have that right?

    Anyhoo, please let me know what I’m doing wrong.

    —JC

    <?
    /* 
    Template Name: Search
    */
    
    get_header(); ?>
    
    <div id="search" class="section p-search">
       <div class="fullwidth triangle">
          <div class="subtlecircle sectiondivider faicon">
            <span class="fa-stack">
            <i class="fa fa-circle fa-stack-2x"></i>
            <i class="fa fa-search fa-stack-1x"></i>
            </span>
          </div>  
          <h5 class="icon-title">Search</h5>
        </div>
      <div class="container ">
    
    					<p class="nb nodisplay">Please note that this page only searches for stuff in the fabulous <a href="/blog">blog</a> and the <a href="/newsletters">newsletters</a>. Everything else, such as the <a href="/#gallery">gallery</a>, information on <a href="/#who">me</a> and how I can help <a href="/#what">you</a>, etc. can be found on the <a href="/">home page</a>.</p>	
    
       
      <?php if (have_posts() ||  $_GET['s'] == '' ) : // the loop ?>
    
    				<? 
    					if( $sq == '') {
              	$ph = 'Type you want to search for here, then press enter...';
    						
    				} else { 
    						$ph = 'Search Again'; 
    						if( $_GET['s'] !== '')
    						    $ph = '';
      		    //    $ph = $wp_query->found_posts . ' results found for: ' . '&quot;' . $sq . '&quot;' .'. Search Again.';
    					} 
    				?>          
                     
         <div class="search-page-wrapper">
              <form method="get" id="searchform" action="<?php bloginfo('home') ?>">
                <input name="s" id="s" type="text" placeholder="<? echo( $ph );?>">
              </form>
            </div>	         
    
    		  <? $gsq = strtolower( get_search_query() ); 	?>	 
    
     			<? if( $gsq == 'quote' || $gsq == 'quotation' ) {
    					 $pTitle = 'Get a Quote';									 
    					 $pLink .= '#quote';									 
    					 
               echo('<div class="postwrap"><div class="post"><h4 id="quote"><a class="quotation" title="Get a Quote now!" href="#quote">Get a Quote now!</a><a title="Relevance" class="relevance-weight">(50)</a></h4>Fill out this simple form and we\'ll get back to you with an estimate!</div></div>');
    	 			 }
    			?> 							 
    
    			<?php while (have_posts()) : the_post(); // the loop ?>
    
              <div class="postwrap">
           
                 <!--post title as a link-->
                 <? $pType    = get_post_type();
    							  $pTitle   = get_the_title();
    								$pLink    = get_the_permalink();
    								$pExcerpt = get_the_excerpt();
    								 
    								if( rtrim($pLink, '/') == rtrim( get_site_url(), '/') ) {
    									 $pTitle = 'Home Page';
    
    									 if( $gsq == 'gallery' ) {
    										 $pTitle = 'Gallery (Home Page)';
    										 $pLink .= '#gallery';
    										 $pExcerpt = 'Some samples of recent work.';
    									 }
    
    									 if( $gsq == 'bio' ) {
    										 $pTitle = 'Bio (Home Page)';
    										 $pLink .= '#who';
    										 $pExcerpt = 'My background.';										 
    									 }
    
    									 if( $gsq == 'tools' ) {
    										 $pTitle = 'Tools (Home Page)';
    										 $pLink .= '#tools';
    										 $pExcerpt = 'My tools.';										 										 
    									 }
    
    									 if( $gsq == 'contact' ) {
    										 $pTitle = 'Contact (Home Page)';
    										 $pLink .= '#contact';
    										 $pExcerpt = 'Fill out this short form for more information or call <a href="2069490892">(206) 949-0892</a> right now!';										 										 										 
    									 }
    									 
    								 }
    							?>
                  
                  
                  <h4 id="post-<?php the_ID(); ?>"><a class="<?php echo($pType); ?>" title="<?php echo($pTitle); ?>" href="<?php echo($pLink); ?>"><?php echo($pTitle); ?></a><a title="Relevance" class="relevance-weight">(<? echo round($post->relevance_score) ?>)</a></h4>
                 <? if( $pType == 'post') { ?>
    							<ul class="post-categories"> 
    							  <? the_category(', '); ?>
    							</ul>	 
                  
    						 <? } ?>
    
                <div class="post ">
                
               
    						        				
                  <? // post text with the read more link ... sorta
                  echo( $pExcerpt);
    							
    							// echo($post->original_excerpt);
    
                  if(strstr($post->post_content,'<!--more')) { //if the story has the more tag... 
    							?>
    	              <p class="post-more"><a href="<?php the_permalink(); ?>">Read the rest of this entry &raquo;</a></p>
                <?php } ?>
            
                <!--post meta info-->
            
               </div> <!--post-->
            
            </div> <!--postwrap-->
    
    	
          <?php endwhile; // end of one post ?>
    
        
          <?php else : // do not delete ?>
            <div class="postwrap">
    
    	  		<p class="subtitle">Oops!</p>
            <p class="search_results">No results. Nothing. A black, empty void of fear, uncertainty and doubt.</p>
    
            
            <form id="searchform" action="http://jchwebdev.com" method="get">
    						<input id="s" type="text" name="s" placeholder="Search Again">
    				</form>
            
            <p class="search_results">Please try again with another search term or just <a href="#contact">contact</a> me and we'll get to the bottom of this!</p>
            
            </div>
    
    	<?php endif; // do not delete ?>
      
         <!-- Previous/Next page navigation -->
         <p class="page-nav">
    	     <?php previous_posts_link('&larr; Previous') ?><?php next_posts_link('Next &rarr;') ?>
         </p>    
    
    		<? blog_sidebar(); ?>
    
    	</div>
      
      
      </div>
      
    <!--include footer-->
    <?php get_footer(); ?>
    Plugin Author Mikko Saari

    (@msaari)

    Correct, so if you have the filter on both hooks, it runs when indexing (so that extra content goes to the index) and when searching (so that excerpts can show the extra content).

    I don’t know, your search results template looks fine. I can’t really tell what’s wrong, at least without tinkering with the site and seeing how the process goes.

    Thread Starter suntower

    (@suntower)

    You are welcome to examine the site, of course. Just let me know what you need to proceed.

    TIA,

    —JC

    Plugin Author Mikko Saari

    (@msaari)

    Unfortunately I don’t have the resources to offer that level of service to users who are not Relevanssi Premium customers.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Problem With relevanssi_excerpt_content filter’ is closed to new replies.