Reply To: Custom field in search results

#6794
Ernest Marcinko
Ernest Marcinko
Keymaster

Hi Tim,

It’s nice to hear from you 🙂

1. Well this issue is not related to the search, but I guess I can help you with it. So I’ve notice you are using the single-publicatie.php file in your theme folder to display these values. The “Array” output there means that it’s not a single value stored but an array of something. After some debugging I found that it’s an array of the post ID in it, so I’ve changed the code on line 51 to this:


<p>Deel van: <?php 
  $related_p_ids = get_post_meta($post->ID, 'deel_van', true);
  $to_echo = array();
  foreach ($related_p_ids as $pid) {     
    $to_echo[] = "<a href='".get_permalink($pid)."'>" . get_the_title($pid) . "</a>";
  }
  if ( count($to_echo) > 0 )
    echo implode(", ", $to_echo);
?></p>

What this does:
– Gets the array of the ids to the $related_p_ids variable
– Gets the permalink and title and forms a link element, then adds it to the $to_echo array
– Implodes the $to_echo array with a comma “, ” delimiter, in case multiple posts were found to print nicely
– Prints the links with on the same line.

2. With a filter function easily. I’ve already appended this to your functions.php file:


add_filter( 'asp_result_author_after_prostproc', 'asp_author_from_cf', 1, 2);

function asp_author_from_cf($author, $post_id) {
  $cf_author = get_post_meta($post_id, 'auteur', true);
  if ($cf_author != false)
    return $cf_author;
  return $author;  
}

Right now the “auteur” fields are empty, but if you start adding text there, it will appear instead of the default author field. Don’t forget to turn back on the “Show author in results” option on the Layout options panel as well.

Best,
Ernest Marcinko

If you like my products, don't forget to rate them on codecanyon :)