Home › Forums › General Issues › query_posts not searching for text inside the post
Hi,
I am new to ACF and new to PHP itself. Since we don’t have the resources to hire an expert, I have to solve this issue myself.
We have a search function on the website in which the search results match a post-type with the searched term. My problem is, the search result looks for the term in the post title and the opening post text. It does not search for the term in the content in the ACFs. Below is the code. It would be great if you could help.
<?php
$args = array(
'post_type' => 'suited-trips',
's' => $_GET['s']
);
query_posts($args);
if ( have_posts() ) :
while(have_posts()) : the_post();
$img_id = get_post_thumbnail_id($post->ID);
$img_src = wp_get_attachment_image_src($img_id,full, true);?>
<div class="<?php echo (++$j % 2 == 0) ? 'right-half-cont' : 'left-half-cont'; ?>">
<div class="image-cont">
<a href="<?php the_permalink();?>"><img src="<?php echo etheme_get_resized_url($img_id, 557, 193, true);?>"></a>
</div>
<div class="list-text-cont">
<a href="<?php the_permalink();?>"><h3><?php the_title(); ?></h3></a>
<p><?php the_content(); ?></p>
</div>
</div>
<?php echo (++$i % 2 == 0) ? '<div class="clear"></div>' : ''; ?>
<?php endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
wp_reset_query();
?>
If you want to filter the search based on the custom field, you can take a look at this page: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/.
That page is talking about archive pages, but the logic should be the same. You can set an attribute to the search URL and get it using the $_GET method. To query based on custom field, please take a look at this page: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/.
I hope this helps 🙂
If you want to search for custom fields you should use a plugin called Relevanssi (https://wordpress.org/plugins/relevanssi/).
This plugin enables the standard wp search to look for terms inside custom fields also (it basically overrides the standard wp search).
There is a setting in relevanssi that you can enable in order to make all custom fields searchable.
Yeah, Relevanssi is a great plugin to search the custom field value in the search string. Please take a look at this page too: http://www.relevanssi.com/knowledge-base/add-custom-fields-search-excerpts/.
Thanks!
The topic ‘query_posts not searching for text inside the post’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.