• themorkdaddy

    (@themorkdaddy)


    Hi, I’m attempting to use your plugin to get search to work with my custom shortcodes. Any time I turn on “Expand shortcodes when indexing,” the pages with shortcodes are indexed (confirmed via tracking indexed post count with and without shortcodes), but those pages with the shortcodes do not appear in searches at all. As soon as I remove the shortcodes from the pages, they appear in the results. Since the indexing is completing without error, I assumed this wasn’t an issue with new WP_Query($search_query).

    Wordpress 5.0.3
    Relevanssi 4.1.3
    custom theme

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Can you find the pages by their titles? What do your custom shortcodes do? Do the pages have other content than the shortcodes?

    Thread Starter themorkdaddy

    (@themorkdaddy)

    In this case, the page title is “Staff.” Searching by page title does not bring it up in results.

    The custom shortcodes are from an older version of the plugin Staffer (v 1.3) that I customized (we didn’t like the new version). The plugin allows you to create departments and add staff members via a custom post type. Once you add the members and put them in departments, you can populate a page with a shortcode, such as [staffer department=”staff-members” orderby=”menu_order” order=”ASC”]. The pages pull in headers, footers, sidebars, etc. If you mean is there other content in the text, I tested that. Regardless of what’s in the text box, if you add the shortcode, the page doesn’t appear in the list. If I turn off Relevanssi, the page appears in searches, but it just displays the shortcode itself (if you search by page title).

    Plugin Author Mikko Saari

    (@msaari)

    Sounds like a potentially problematic shortcode. If there’s a post loop inside it, it may confuse Relevanssi indexing.

    If you disable shortcode expansion in Relevanssi settings, can you find the pages by page title? If you can, then the problem is indeed in the shortcode.

    As for the shortcode, it’s quite hard to say what’s wrong without seeing what the shortcode does.

    Thread Starter themorkdaddy

    (@themorkdaddy)

    Shortcode does indeed use a post loop.

    Disabling shortcode expansion does not allow me to find the pages by page title.

    Here’s what the shortcode does:

    // adds staff shortcode
    if( !function_exists( 'staffer_shortcode' ) ) {
    	function staffer_shortcode( $atts ) {
    		ob_start();
    		extract( shortcode_atts( array (
    			'order' => 'DESC',
    			'orderby' => 'date',
    			'number' => -1,
    			'department' => '',
    		), $atts ) );
    
    		if ( $department != '' ) { 
    			$tax_query = array ( 
    				array (
    					'taxonomy'	=> 'department',
    					'field'		=> 'slug',
    					'terms'		=> $department,
    					),
    				);
    		} else {
    			$tax_query = null;
    		}
    		$args = array(
    			'post_type' => 'staff',
    			'order' => $order,
    			'orderby' => $orderby,
    			'posts_per_page' => $number,
    			'tax_query' => $tax_query,
    		);
    		$staff_query = new WP_Query( $args );
    
    		$max_columns = 2;
    		$column = 12/$max_columns;
    		$i = 0;
    		$x = 0;
    
    		if ( $staff_query->have_posts() ) : 
    			
    			global $post;
    			$stafferoptions = get_option ( 'staffer' );
    			while ( $staff_query->have_posts() ) : $staff_query->the_post();
    
    			if (isset ($stafferoptions['gridlayout']) ) : 
    
    				if (($i % $max_columns) == 0 ) : 
    					$no_columns = '<div class="row">';
    					else :
    						$no_columns = '';
    				endif; ;
    
    				if (isset ($stafferoptions['gridlayout']) ) :					
    					if ($stafferoptions['estyle'] == null or $stafferoptions['estyle'] == 'excerpt' ) :
    							the_excerpt();
    					elseif ($stafferoptions['estyle'] == 'full' ) :
    							the_content();
    					elseif ($stafferoptions['estyle'] == 'none' ) :
    							// nothing to see here
    					endif;
    				endif;				
    			
    				if ( get_post_meta ($post->ID,'staffer_staff_title', true) != '' ) :
    					$position = get_post_meta ($post->ID,'staffer_staff_title', true) . '<br />' ;
    				endif;
    
    				if ( get_post_meta ($post->ID,'staffer_staff_company', true) != '' ) : 
    					$companyFill = 'Company: ' . esc_html( get_post_meta ($post->ID,'staffer_staff_company', true) ) . '<br />';
    				endif;			
    
    				if ( get_post_meta ($post->ID,'staffer_staff_phone', true) != '' ) : 
    					$phoneFill = 'Phone: ' . esc_html( get_post_meta ($post->ID,'staffer_staff_phone', true) ) . '<br />';
    				endif;
    				
    				if( $fax):
    					$faxFill = 'Fax: ' . $fax . '<br />';
    				endif;
    				
    				if ( get_post_meta ($post->ID,'staffer_staff_email', true) != '' ) : 
    					$email = get_post_meta ($post->ID,'staffer_staff_email', true); 
    				endif;
    				
    				if( $email ): 
    					$emailFill = '<a href="mailto:' . $email . '">' . $email .'</a><br />';
    				endif;
    
    				// write the code
    				echo $no_columns . '<div class="col-sm-6"><div class="row"><div class="col-md-4 col-sm-4 col-xs-4">' . get_the_post_thumbnail ( $post->ID, 'thumbnail', array ('class' => 'staffer-img') ) . '</div><div class="col-md-8 col-sm-8 col-xs-8"><strong>' . $position . '</strong>' . get_the_title() .'<br />' . $companyFill . $phoneFill . $faxFill . $emailFill . '</div></div><hr></div>';
    
    				$i++;
    				if (($i % $max_columns) == 0 ) : 
    					echo '</div>';
    				endif;
    
    			endif;
    
            	endwhile;
    
            	wp_reset_postdata();
    
    			if (($i % $max_columns) != 0 ) : 
    				echo '</div>';
    			endif;
    
    		endif;
    			
    
    		$output = ob_get_clean();
    		return $output;
    		}	
    	}
    
    add_shortcode( 'staffer', 'staffer_shortcode' );
    Plugin Author Mikko Saari

    (@msaari)

    Using WP_Query like that is not the recommended method; since you’re not in a template, it would be better to use get_posts(). It takes the same parameters, but returns an array of post objects – that’s much easier to handle, as well.

    So, instead of

    $staff_query = new WP_Query( $args );

    you’d have

    $staff_query = get_posts( $args );

    and instead of

    while ( $staff_query->have_posts() ) : $staff_query->the_post();

    you’d have

    foreach ( $staff_query as $staffer ) {

    Since you don’t set up the global post object, you can’t use the_excerpt() or the_content(), but you can generally use $staffer->post_excerpt and $staffer->post_content instead.

    If you’d convert the shortcode function like that, it’d work the same way as it does now, except it would also work with Relevanssi.

    Thread Starter themorkdaddy

    (@themorkdaddy)

    Okay, I got that to work…sort of.

    At this point, the plugin indexes. If I do a search for the page title, it now appears, and it displays an excerpt with content it expanded from the shortcode. However, let’s say there’s someone named John Smith on the staff. His name will appear in the staff page excerpt as mentioned above (when you search for the page title “Staff,” but if you do a search with just his name, it comes back empty. It’s like it’s indexing the page before it finishes building the excerpt from the shortcode.

    I’ve set Relevanssi to expand the shortcodes when indexing, run indexing with “Index the post excerpt” both off and on. Is there any way to get it to search the excerpts that it displays in the search results?

    Plugin Author Mikko Saari

    (@msaari)

    The excerpts and the index are two completely separate things which have nothing to do with each other. The excerpt is built with a separate process when the search is made, and if something shows up in the excerpt, it has nothing to do with searching.

    Do this:

    1. Create an empty page.
    2. Add the shortcode there as the only content of the page.
    3. Publish the page privately.
    4. Check wp_relevanssi table rows where doc is the page ID. What do you see? Is there anything there?

    Sounds like the shortcode isn’t returning the content for Relevanssi to see.

    With this post, you can then debug the process. In your shortcode function, before the return $output; line, you can put

    var_dump($output);
    exit();

    and then go save the page again. That should print out how Relevanssi sees the shortcode content. Are you seeing the correct content there?

    Thread Starter themorkdaddy

    (@themorkdaddy)

    Okay. First let me say thank you for your patience and time. Here’s what I discovered.

    I didn’t realize the free version Relevanssi doesn’t support custom fields (we use ACF). After looking at the wp_relevanssi table and seeing the shortcode text showing up under customfield, I figured it had to do with Advanced Custom Fields. So, I put the shortcode in the regular content editor, and it became completely searchable.

    We had put in our own custom php to extend WordPress search into custom fields, but while it does extend searches to those fields for regular content, it doesn’t work on shortcodes. We found Relevanssi when looking for a plugin solution, but unless it’s part of the premium version, it looks like Relevanssi doesn’t expand shortcodes in custom fields, either. Is that an option in Premium?

    Plugin Author Mikko Saari

    (@msaari)

    The free version of Relevanssi very much supports custom fields; Premium has the same features for custom fields are the free version does, with couple of differences, and those do not affect this.

    Neither version expands short codes in custom fields, because generally that’s just not a thing people do =)

    What you need is something like this:

    add_filter( 'relevanssi_custom_field_value', 'rlv_shortcodes_in_custom_fields' );
    function rlv_shortcodes_in_custom_fields( $value ) {
        $value = do_shortcode( $value );
        return $value;
    }

    That should expand all shortcodes in custom fields.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Expand shortcodes when indexing not working’ is closed to new replies.