adding custom fields and excerpts to results
-
Hello,
I’ve been trying to figure out the best way to show my search results using your plugin. The way my site is set up, it is all pages, built with a themes (kallyas) page builder. I got the custom field part working for searches by putting “zn_meta_elements” in “custom fields to index”. This seems to encompass EVERY custom field I have and would like to narrow it down but I’m not sure how. There doesn’t seem to be a subfield specifically. This is the code I’m looking at:
$zn_meta_elements[] = array ( "name" => "Text Box", "description" => "Text Box", "id" => "_text_box", "std" => '', "type" => "group", "add_text" => "Item", "remove_text" => "Item", "hidden" => true, "sizes" => "four,one-third,eight,two-thirds,twelve,sixteen", "link" => true, "subelements" => array ( array ( "name" => "Sizer Hidden Option", "desc" => "This option is hidden.", "id" => "_sizer", "std" => "four", "type" => "hidden", "class" => 'zn_size_input' ), array ( "name" => "Title", "description" => "Please enter the title for this box", "id" => "stb_title", "std" => "", "type" => "text", ), array ( "name" => "Content", "description" => "Please enter the box content", "id" => "stb_content", "std" => "", "type" => "textarea", ), array( "name" => "Title style", "description" => "Select the desired style for the title of this box", "id" => "stb_style", "type" => "select", "std" => "style1", "options" => array ( 'style1' => 'Style 1', 'style2' => 'Style 2', 'callout' => 'callout', 'divider' => 'divider', 'subpage' => 'subpage'), ) ) );
Ideally, I’d want to somehow make it so that search only reads “callout” and “subpage” but if I have too, I’d settle for the text box in general.
Also, I cannot seem to get the excerpts to work at all. Part of me is wondering if it’s because I cannot get the first part right. But I’m not quite sure. My search results show the ENTIRE page that matched, not an excerpt. I’ve tried the following:
add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3); function excerpt_function($content, $post, $query){ $relevanssi_index_post_types = relevanssi_get_custom_fields(); if(!empty($relevanssi_index_post_types)){ $meta_values = ''; foreach($relevanssi_index_post_types AS $custom_post_type){ $meta_value = get_post_meta( $post->ID, $custom_post_type, true); if(!empty($meta_value)){ $meta_values .= $meta_value.' '; } } } if(!empty($meta_values)){ $content .= preg_replace("/\n\r|\r\n|\n|\r/", " ", $meta_values); } return $content; }
and
add_filter('relevanssi_excerpt_content', 'custom_fields_to_excerpts', 10, 3); function custom_fields_to_excerpts($content, $post, $query) { $custom_field = get_post_meta($post->ID, 'zn_meta_elements', true); $content .= " " . $custom_field; return $content; }
Just to name a few…
Any advice you could give would be so greatly appreciated.
Thank you!
- The topic ‘adding custom fields and excerpts to results’ is closed to new replies.