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

    (@msaari)

    Sure. How exactly would you like it to work?

    Thread Starter Adrien Lamotte

    (@adrienlamotte)

    Well, maybe we can use a hook. But i’m not sure which one is the better pick ? Maybe relevanssi_content_to_index ?

    But after that, I don’t know how to do ?

    My relationship fields returns an arrays of ids. So if we can get that array, maybe we can add the content of each items to the page search (those are custom post type ids).

    Thanks for your help ! ??

    Plugin Author Mikko Saari

    (@msaari)

    Ok, so you want to add the content of the related items to the post they are related to? In that case relevanssi_content_to_index is the way to go. You can use get_field() to get the ACF field values and go from there.

    Thread Starter Adrien Lamotte

    (@adrienlamotte)

    Thank you for the help. I got it working !

    Here is a simplified version of what I did (cause my ACF structure is more complexe) :

    add_filter('relevanssi_content_to_index', 'beautymed_add_product', 10, 2);
    add_filter('relevanssi_excerpt_content', 'beautymed_add_product', 10, 2);
    
    function beautymed_add_product($content, $post) {
    
        $tplName = get_post_meta( $post->ID, '_wp_page_template', true );
    
        if ($tplName === 'template-types.php') {
    
            $products = get_field('produits');
    
            foreach ($products as $product) {
                $content .= get_field('description', $product->ID);
                $content .= get_field('infos', $product->ID);
                $content .= get_field('conseils', $product->ID);
            }    
    
        }
    
        return $content;
    }

    Thank you both! Helped me quite a bit. I’m using CMB2’s “Related Posts Field” which stores a single ‘related’ post ID. Here’s what worked for me:

    add_filter('relevanssi_content_to_index', 'MY_include_related_content', 10, 2);
    add_filter('relevanssi_excerpt_content', 'MY_include_related_content', 10, 2);
    
    function MY_include_related_content($content, $post) {
    
        $related = get_post_meta( $post->ID, 'custom_field_with_post_id', true );
    
        if (is_numeric($related)) {
    		$content .= apply_filters('the_content', get_post_field('post_content', $related));
    	}
    
        return $content;
    }
    

    @mikko also posted this over on the blog post that lead me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘ACF Relationship field’ is closed to new replies.