0

I asked this question on stack overflow but haven't had any joy thus far. Link: https://stackoverflow.com/questions/41716882/wordpress-search-form-and-search-result-through-acf-field-in-custom-taxonomy/41718983#41718983

Copy of the question below:

So I think I'm making life hard for myself.

I have a search field on my website that I have split into 3 e.g:

<input type="text" name="part1" />
<input type="text" name="part2" />
<input type="text" name="part3" />

On my results page I create the search string e.g:

$searchstring = $part1.'-'.$part2.'-'.$part3;

The process then is to search the database for my custom field that has a value of $searchstring

I found a search function https://gist.github.com/jserrao/d8b20a6c5c421b9d2a51 there which I think's rather close to what I'm trying to achieve but I'm not really sure how to implement everything in.

My data roughly looks like this:

(taxonomy) product_cat - (name) Category 1 - (custom field) gc_number - (value I need to search) 77-999-77
(taxonomy) product_cat - (name) Category 2 - (custom field) gc_number - (value I need to search) 73-333-73
(taxonomy) product_cat - (name) Category 3 - (custom field) gc_number - (value I need to search) 76-666-76

I then need to display the product_cat name to the user.

Hopefully this makes some sense and any help would be greatly appreciated!

1 Answer 1

0

This should hopefully help you on your way, although I've not fully tested it...

The basic search form here which will load the site_url() with appended search query string. You should change this to better reflect your site layout such as <?php site_url( 'search' ); ?> ('http://website.com/search').

Place the Results page code within the results page template (based on the site_url() code above). This will collect all parts (inputs), turn them into a string, then use them in a custom query using the meta_query parameter.

It's basically saying "Find all products where gc_number equals the search string". You can then loop through all results and output whatever you need.

If it's not quite what you're after, or you need a hand, drop a comment below.

<form role="search" method="get" id="acf-search" action="<?php site_url(); ?>">
    ...

    <input type="text" name="part1" />
    <input type="text" name="part2" />
    <input type="text" name="part3" />

    <input type="submit" value="Search" />
</form>

<?php
// Results page

// Might be worth checking to make sure these exist before doing the query.
$part_1 = $_GET['part1'];
$part_2 = $_GET['part2'];
$part_3 = $_GET['part3'];
$search_string = $part_1 . '-' . $part_2 . '-' . $part_3;

// Get all product categories
$categories = get_terms( 'product_cat' );
$counter = 0;

// Loop through categories
foreach ( $categories as $category ) {

    // Get custom field
    $gc_number = get_field( 'gc_number', $category );

    // If field matches search string, echo name
    if ( $gc_number == $search_string ) {
        // Update counter for use later
        $counter++;
        echo $category->name;
    }
}

// $counter only increases if custom field matches $search_string.
// If still 0 at this point, no matches were found.
if ( $counter == 0 ) {
    echo 'Sorry, no results found';
}
?>
4
  • Hi Kieran, thank you very much for your answer! Unfortunately it's not a product loop i need to do, the custom field is assigned to a taxonomy. It's the Woocommerce categories whose taxonomy is product_cat so i have say 3 product_cats which each have a custom field gc_numbers. It's the value of gc_numbers I need to search then display the name of the product_cat e.g. Category 1. Hopefully this make some sense. It's a bit long winded! Lol Commented Jan 24, 2017 at 10:45
  • Ah, I misunderstood that. I've updated the code now and it should work how you need it to (I think). It might not be most graceful way to do this as it's querying all categories, looping through each and comparing it to the search string, but it should work fine. Commented Jan 24, 2017 at 11:24
  • Ah Kieran that's brilliant! It's working and getting the category name. I added an else in to display 'nada' if nothings returned (to try it out) and I got nadanadanadaCategory 1nadanada :s lol. It works perfectly without my else. Commented Jan 24, 2017 at 11:58
  • I'm an idiot, I missed the bottom of your code! Thanks so much! Commented Jan 24, 2017 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.