0

I understand the Premium version of Relevanssi has support for ACF fields built in but I have the free version. I'm working on a way to grab the information from the database and then spit it out on the search results page. This is what I have come up with so far:

/************** FUNCTIONS.PHP ****************/

// Relevanssi does not show ACF fields - this is a work-around for specific fields in the DB
function excerpt_function($content, $post, $query) {
    global $wpdb; 

    // Grab everything in the ACF content fields from the DB and spit it out in the search result excerpt. If new ACF modules/fields are added in the future then they need to have their 'modules_#_name_name_content' and so on added to show up in the Relevanssi search.
    $fields = $wpdb->get_col("SELECT DISTINCT(meta_value) FROM $wpdb->postmeta WHERE meta_key LIKE '%modules_%'");
    foreach($fields as $key => $field){ 
        $field_value = get_post_meta($post->ID, $field, TRUE); 
        $content .= ' ' . ( is_array($field_value) ? implode(' ', $field_value) : $field_value ); 
    }
    return $content; 
}
add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3);

I think I need help with:

$fields = $wpdb->get_col("SELECT DISTINCT(meta_value) FROM $wpdb->postmeta WHERE meta_key LIKE '%modules_%'");

The name of the ACF content in the database always starts with "modules_". If I can figure out how to get the meta value from the meta key with prefix "modules_" then I think I can spit out the results on the search page.

Here is the important part of my search results:

/************** SEARCH.PHP ****************/

<h2><?php 
    global $wp_query;
    $some_results = $wp_query->found_posts;
    printf( __( 'There are ' . $some_results . ' Search Results for: %s', 'eqh' ), '<strong>' . get_search_query() . '</strong>' ); ?>
</h2>
<hr />
<?php if ( have_posts() ) :
    while ( have_posts() ) : the_post(); ?>

        <div class="large-3 medium-6 small-12 columns m-bottom">
            <article class="striped clearfix" style="border:1px solid #ccc;">
                <h3><a href="<?php echo get_permalink(); ?>"><?php relevanssi_the_title(); // the_title(); ?></a></h3>

                <?php if( has_post_thumbnail() ){ ?>
                    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' ); // https://stackoverflow.com/questions/32837968/how-to-get-featured-image-of-a-product-in-woocommerce ?>
                <div class="m-bottom f-left m-right"><a href="<?php echo the_permalink(); ?>"><img src="<?php echo $image[0]; ?>" data-id="<?php echo $loop->post->ID; ?>"></a></div>

                <?php } else { ?>

                    <div class="m-bottom f-left m-right"><img src="<?php echo site_url(); ?>/images/placeholder.png" width="" height="" alt="placeholder png" /></div>

                <?php } ?>

                <br clear="all" />
                <?php echo the_excerpt(); // get_the_excerpt(); ?>
                <p><a href="<?php echo the_permalink(); ?>"><?php _e( 'Read More', 'eqh'); ?></a></p>
            </article>
        </div>

    <?php endwhile;
endif; ?>
  • The database query is now: $fields = $wpdb->get_col("SELECT DISTINCT(meta_value) FROM $wpdb->postmeta WHERE meta_key LIKE '%modules_%'"); but it spits out ARRAY ARRAY ARRAY ARRAY ARRAY ... on the page. Thoughts? – S. Jensen Apr 4 at 21:14
  • This is what the database SQL looks like: SELECT * FROM XXXXXXXXXXXX.XXX_postmeta WHERE (CONVERT(meta_id USING utf8) LIKE '%modules_%' OR CONVERT(post_id USING utf8) LIKE '%modules_%' OR CONVERT(meta_key USING utf8) LIKE '%modules_%' OR CONVERT(meta_value USING utf8) LIKE '%modules_%'). I'm trying to put together something from this. – S. Jensen Apr 4 at 21:19
  • I'm here now with my $wpdb query $fields = $wpdb->get_col("SELECT DISTINCT(meta_key) FROM $wpdb->postmeta WHERE meta_key LIKE '%_content' AND NOT '_modules%'"); but I can't figure out the second part AND NOT '_modules%' doesn't prevent meta_values from meta_keys beginning with _modules% from showing. – S. Jensen Apr 15 at 21:35
  • Just solved it: AND meta_key NOT LIKE '_modules%'"); is the correct syntax to prevent meta_value from meta_key I don't want. – S. Jensen Apr 15 at 21:49
0

I'll post only the FUNCTIONS.PHP code:

// Relevanssi does not show ACF fields by default - this is a work-around for specific fields in the DB
function excerpt_function($content, $post, $query) {
    global $wpdb; 

    // Grab everything in the ACF content fields from the DB and spit it out in the search result excerpt. If new ACF modules/fields are added in the future then they need to have their 'modules_#_name_name_content' and so on added to show up in the Relevanssi search.
    $fields = $wpdb->get_col("SELECT DISTINCT(meta_key) FROM $wpdb->postmeta WHERE meta_key LIKE '%_content' AND meta_key NOT LIKE '_modules%'");
    foreach($fields as $key => $field){ 
        $field_value = get_post_meta($post->ID, $field, TRUE); 
        $content .= ' ' . ( is_array($field_value) ? implode(' ', $field_value) : $field_value ); 
    }
    return $content; 
}
add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.