Support

Account

Home Forums General Issues query_posts not searching for text inside the post

Solving

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();
    			   ?>  
  • Hi @indian-outskirts

    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.

  • Hi @adser @indian-outskirts

    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!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘query_posts not searching for text inside the post’ is closed to new replies.