Advanced Custom Fields Relationship fields

I’m working on a website and i’m use A LOT the Relationship field au ACF.

So on my pages are displayed informations of some related custom post.

Is there a way to get that working in the search ?

With some help from me, the person who asked this question on the WordPress.org support forums came up with this code to include related posts connected through ACF Relationship field:

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;
}

(Original question.)

  • Will

    Here’s a simple example for you have only a ‘related’ post ID stored.


    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;
    }