Support

Account

Home Forums General Issues Adding custom fields search to WordPress search

Solved

Adding custom fields search to WordPress search

  • I have searched quite a lot for this fix but for the life of me can’t find it and wondered if anyone here could help?

    I am using Advanced Custom Fields 4.4.2 to store a list of business types for a particular Post (Customer). For each post a custom field called “business type” will have a number of values that can be selected i.e. Dr’s, Surgery, Doctors, First Aid etc.

    I would like to be able to include the “business-type” custom field in the search so that all posts (customers) that have this business type will show, alongside any post with that text in is also (which may not contain the custom field in it).

    I have tried using a function but I guess this turns the query into an “AND” statement rather than an “OR” (Join) statement.

    add_action(‘pre_get_posts’, ‘modify_post_search’);
    function modify_post_search($query)
    {
    if($query->is_search)
    {
    $query->set(‘meta_key’, ‘business_type’);
    $query->set(‘meta_value’, get_search_query());
    }
    }

    add_filter(‘pre_get_posts’, ‘filter_search’);
    ;

  • Have you tried a plugin like this one https://wordpress.org/plugins/search-everything/

  • Hi John, I didn’t really want to use that plugin as it looked like it would lose control over a few things.

    So, I did another search today and this was worked first time. http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/

    Really handy as I just add it to the functions.php. It’s adding an JOIN to the query but I haven’t noticed much in the way of speed loss so I’m going with this.

  • I recommend Relevanssi for this. The advantage of using Relevanssi is that it pre-indexes your custom fields (and thus keeps your public searches up to speed).

    Use your imagination expanding the following code sample:

    
    function relevanssi_add_custom_fields( $fields )
    {
    	$fields = array(); // I like to start fresh
    	$fields[] = 'text_field';
    	$fields[] = 'people_0_firstname'; // meta key for 'firstname' field inside 'people' repeater.
    
    	$fields = implode( ',', $fields );
    
    	return $fields;
    }
    add_filter( 'option_relevanssi_index_fields', 'relevanssi_add_custom_fields' );
    
  • Hi EssexPaulo,

    Not sure if this is what you are after but have you tried by modifying your search.php file on your theme to something like this:

    
    <?php
    $customers = get_posts(array(
                                'post_type' => 'customers',
                                'posts_per_page' => -1,
                                'nopaging' => true,
                                'meta_query' => array(
                                    array(
                                        'key' => 'business_type',
                                        'value' => '"' . get_search_query()  . '"',
                                        'compare' => 'LIKE'
                                    )
                                )
                            ));
    
    ?>
    
    <?php /* Loop into the customers array and display the results */ ?>
    <?php foreach($customers as $customer_post) : ?>
    
       <?php /* Show your results */ ?>
       <?php setup_postdata($customer_post); ?>
    
    <?php endforeach; ?>
    
    <?php /* Keep standard search loop for any other posts */ ?>
    <?php while (have_posts()) : the_post(); ?>
    
      <?php /* Here remains the rest of the original search.php */ ?>
    
    <?php endwhile; ?>
    

    Cheers,

    Milton

  • Thanks for your help guys but the reply I put has sorted it very well. No bother with having to edit any files other than functions.php (which for a child theme is perfect)

    This was the link again http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/

    Thanks again for your help.
    Paul

  • Cool Find Paul,

    But maybe not everyone is to confident coding. There is a plugin FacetWP which is easy to configure and works well with Advanced Custom Fields.

  • If someone is looking for a solution to this problem is to recommend the plugin:
    https://wordpress.org/plugins/acf-better-search/

    This plugin adds to default WordPress search engine the ability to search by content from selected fields of Advanced Custom Fields plugin.

    Everything works automatically, no need to add any additional code.

  • For anyone else looking for a solution and doesn’t want to write code or download a third third party plugin, use this ACF add-on plugin: https://wordpress.org/plugins/acf-better-search/

    Cheers.

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.