• Resolved goldenatlas

    (@goldenatlas)


    I’m using Advanced Custom Fields to structure content in custom posts in WordPress. Relevanssi is working wonderfully for the most part by setting the “custom fields to index” field in the Relevanssi settings to “visible.” The problem, as described in the User Manual, is that “Relevanssi expects the custom field value to be a string, so if you’re storing serialized objects or arrays in your custom fields, indexing them might not work as expected.”

    I’ve followed these instructions to add custom fields to the excerpt by first converting the array into a string, like so:

    function custom_fields_to_excerpts($content, $post, $query) {
      /*
       * A custom function that returns an associative array of custom taxonomy names, e.g.:
       * array (
       *   [browsers] => array('chrome', 'safari'),
       *   [providers] => array('google', 'yahoo'),
       * )
       */
      $app = my_get_app_filters($post->ID);
    
      // Convert associative array to string (ex: "chrome, safari, google, yahoo")
      $string = my_array_to_string($app['filters']);
      $content .= " " . $string;
    
      return $content;
    }
    add_filter('relevanssi_excerpt_content', 'custom_fields_to_excerpts', 10, 3);

    No dice, though. Any idea how to give Relevanssi insight into custom fields stored in arrays?

    https://www.ads-software.com/plugins/relevanssi/

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

    (@msaari)

    That does add the contents of the arrays to the source material for excerpts. However, that doesn’t help with indexing. I’m not sure what gets in the index from an associative array, if anything…

    However, you can help Relevanssi with that. Add a similar function to ‘relevanssi_content_to_index’ – the same function is fine, except that this filter only has one parameter, $post, not two.

    Try that and see if that helps.

    Thread Starter goldenatlas

    (@goldenatlas)

    Nice! That worked perfectly! Here’s how I added my own blend of custom fields to Relevanssi:

    function add_custom_fields_to_relevanssi($post) {
      /*
       * Do stuff here with the $post object to get your custom field data.
       * Convert any terms inside of arrays or serialized objects into a
       * string at this time, i.e.:
       *
       * $custom_fields = array (
       *   [browsers] => array('chrome', 'safari'),
       *   [providers] => array('google', 'yahoo'),
       * );
       *
       * should be converted to:
       *
       * $custom_fields = "chrome, safari, google, yahoo"
       */
    
      // Add your custom field data to $content
      $content .= " " . $custom_fields;
    
      // Return the content
      return $content;
    }
    add_filter('relevanssi_content_to_index', 'add_custom_fields_to_relevanssi', 10, 3);
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding custom fields stored in arrays to search results’ is closed to new replies.