Support » Plugin: Relevanssi - A Better Search » Wont index ACF Post Object data?

  • Resolved mikedistras

    (@mikedistras)


    Cant figure this one out, and couldnt find anything similar by searching.

    Is there any way to have Relevanssi index a custom post type and the ACF Post Object data inside of it?

    EG. Post Object field called “Profiles” which holds peoples “first name” “last name” etc.
    Custom post type called “Exports”

    So therefor you can add a persons “Profile” to an “Export” custom post type.

    I can search the other fields inside the “Export” post type fine, but i cant get any result at all if i search “Mike”, which would be stored as a “First Name” field within the “Profiles” Post Object.

    Any ideas?

    (PS: Im using relevanssi alongside the Search & Filter Pro plugin)

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

    (@msaari)

    Relevanssi doesn’t probably understand what the post object is all about. You need to add a filter function that reads the contents from the post object and passes them to Relevanssi in plain text format Relevanssi can understand.

    You can use the relevanssi_content_to_index filter hook. It gives you post ID, which you then use to fetch the matching post object, read out the contents and return them as a string. Problem solved!

    mikedistras

    (@mikedistras)

    Thanks for the reply Mikko!

    While researching I found the filter hook you mentioned, however im not too sure how to actually implement it, how would i actually implement that in to the theme/functions.php file?

    Plugin Author Mikko Saari

    (@msaari)

    add_filter('relevanssi_content_to_index', 'add_post_object_data', 10, 2);
    function add_post_object_data($content, $post) {
        $post_object = get the post object based on the $post->ID;
        $content_to_index = extract the content from $post_object;
        $content .= " " . $content_to_index;
        return $content;
    }

    That’s how – I have no idea what those post objects are, where they are stored and how the data is contained in them, so I can’t be more specific, but that’s the general idea anyway.

    mikedistras

    (@mikedistras)

    Thanks for the help again Mikko.

    The Advanced Custom Fields Post Object in the CPT is simply called “customer_field”, and the data i want to index inside the post object are another 2 ACF fields called “first_name” and “surname”.

    Does that help? If you could help with the formatting of your above mentioned code, that would be highly appreciated.

    mikedistras

    (@mikedistras)

    Ah, i managed to get it to (partially) work, the formatting of your post here really helped, thank you!

    The code I used below, helped index 1 defined field from the post object:

    add_filter('relevanssi_content_to_index', 'add_post_object_data', 10, 2);
    function add_post_object_data($content, $post) {
        $post_object = get_field('customer_field', $post->ID);
        $content_to_index = get_field('surname', $post_object);
        $content = $content_to_index;
        return $content;
    }

    However if i try index another field, the first one (surname), breaks and doest find any results…

    add_filter('relevanssi_content_to_index', 'add_post_object_data', 10, 2);
    function add_post_object_data($content, $post) {
        $post_object = get_field('customer_field', $post->ID);
        $content_to_index = get_field('surname', $post_object);
        $content_to_index = get_field('first_name', $post_object);
        $content = $content_to_index;
        return $content;
    }

    How can i get it to index both fields?

    Plugin Author Mikko Saari

    (@msaari)

    Your code is correct, except you’re overwriting the first value with the second value. If you change the assignment to appending, it’ll work. It’s also wise to add some spaces:

    $content_to_index = get_field('surname', $post_object);
    $content_to_index .= " " . get_field('first_name', $post_object);
    $content = " " . $content_to_index;
    mikedistras

    (@mikedistras)

    Great stuff, that worked perfectly and found the results i expected it to, thank you!

    Last issue now is that it doesnt actually display the expected results unless
    I Erase and Rebuild the Cache each time a new post is created. (however the Highest Post Index number does increase correctly).

    Is this just a cache issue, and me being impatient, or is there another way to get the results to display as expected?

    Plugin Author Mikko Saari

    (@msaari)

    That is correct – Relevanssi doesn’t know that when post A is being saved and indexed, post B that shows information from post A should also be reindexed.

    If you can tell when saving post A which other posts should be reindexed, you can add some code that does that.

    If it’s just a single post you want reindexed when other posts are reindexed, the code looks like this:

    add_action('wp_insert_post', 'rlv_update_dependencies', 100, 1); 
    function rlv_update_dependencies($post_id) {
        relevanssi_publish(POST_ID_TO_REINDEX, $true);
    }

    Where POST_ID_TO_REINDEX is the post ID for the post you want to reindex. If the post to reindex depends on the post you’re currently saving, that post ID is stored in $post_id, and you can use that to figure out which post you want to reindex.

    The other approach is to just rebuild the index every day. The easiest way to do that is to use WP CLI and just run wp relevanssi index on the command line once per day with cron. This requires Relevanssi Premium; there are ways to do this with the free version and without WP CLI, but this is the easiest way.

    mikedistras

    (@mikedistras)

    Could you give me some instructions on how to run this as a cron job within the free version at say 2 or 3 times per day? Or point me to where i could find instructions?

    EDIT:
    Sorry, found it in the FAQ’s here: https://wordpress.org/plugins/relevanssi/#faq

    • This reply was modified 2 months ago by  mikedistras.
Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.