admin管理员组文章数量:1023242
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' ); // ?>
<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; ?>
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/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; ?>
Share
Improve this question
edited Apr 4, 2019 at 21:16
S. Jensen
asked Apr 4, 2019 at 20:03
S. JensenS. Jensen
12 bronze badges
4
|
1 Answer
Reset to default 0I'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);
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' ); // ?>
<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; ?>
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/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; ?>
Share
Improve this question
edited Apr 4, 2019 at 21:16
S. Jensen
asked Apr 4, 2019 at 20:03
S. JensenS. Jensen
12 bronze badges
4
- 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 Commented Apr 4, 2019 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 Commented Apr 4, 2019 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 Commented Apr 15, 2019 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 Commented Apr 15, 2019 at 21:49
1 Answer
Reset to default 0I'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);
本文标签: databaseFree version of Relevanssi doesn39t show ACF fields
版权声明:本文标题:database - Free version of Relevanssi doesn't show ACF fields 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745585634a2157577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 Commented Apr 4, 2019 at 21:19