免费版本的Rlevanssi不显示ACF字段

时间:2019-04-04 作者:S. Jensen

我知道Relevansi的高级版本支持内置ACF字段,但我有免费版本。我正在研究一种方法,从数据库中获取信息,然后在搜索结果页面上显示出来。这就是我目前的想法:

/**************功能。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);
我想我需要以下方面的帮助:

$fields = $wpdb->get_col("SELECT DISTINCT(meta_value) FROM $wpdb->postmeta WHERE meta_key LIKE \'%modules_%\'");
数据库中ACF内容的名称始终以“modules\\uu0”开头。如果我能找出如何从前缀为“modules\\uu0”的元键中获取元值,那么我想我可以在搜索页面上显示结果。

以下是我的搜索结果的重要部分:

/**************搜索。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; ?>

1 个回复
最合适的回答,由SO网友:S. Jensen 整理而成

我只发布函数。PHP代码:

// 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);

相关推荐

Database problem

我的数据库有问题。首页工作正常,但当我想访问backoffice mysite时。com/wp admin我正在重定向到/wp admin/install。因此,我打开了phpmyadmin,看到有一个空数据库。。没有列,没有行,只有前端站点和所有页面工作正常吗?XD。发生了什么事?我有w3 total缓存和云flare。也许我的网站只因为我的数据库被缓存而工作?如何恢复数据库?请帮帮我,我没有备份。