Support » Fixing WordPress » WordPress Custom Search

  • This is maddening. I can’t seem to figure out what seems like an easy thing to do. I am building a custom search function for a WordPress site that is selling used RVs. I am using Woocommerce to catalog the products (probably not important since it just creates a product with a post_type=product instead of post_type=post).

    I am using custom fields to define options such as the vehicle’s ‘Class’ (e.g. ‘Class A’, ‘Class B’, ‘Class C’, etc.) and ‘Fuel Type’ (e.g. ‘Gasoline’, ‘Diesel’, ‘Towable’). So each product may have a different meta_value for the same meta_key, but may only have each meta_key defined only once and may only have one meta_value per meta_key.

    Part of the search form looks something like this. (There is more but this all we need to worry about at the moment)

    <h4>Type / Class</h4>
    <label><input type="checkbox" name="a1" value="0" />All Types</label>
    <label><input type="checkbox" name="a2" value="Class A" />"Class A" RV's</label>
    <label><input type="checkbox" name="a3" value="Class B" />"Class B" RV's</label>
    <label><input type="checkbox" name="a4" value="Class C" />"Class C" RV's</label>
    <label><input type="checkbox" name="a5" value="Fifth Wheel" />Fifth Wheel RV's</label>
    <label><input type="checkbox" name="a6" value="Toy Hauler" />Toy Haulers</label>
    <label><input type="checkbox" name="a7" value="Travel Trailer" />Travel Trailer RV's</label>
    <label><input type="checkbox" name="a8" value="Camping Trailer" />Camping Trailers</label>
    <h4>Engine Type</h4>
    <label><input type="checkbox" name="c1" value="0" />All Engine Types</label>
    <label><input type="checkbox" name="c2" value="Gasoline" />Gasoline</label>
    <label><input type="checkbox" name="c3" value="Diesel" />Diesel</label>
    <label><input type="checkbox" name="c4" value="Towable" />Towable</label>

    Which is sent to run the following query and return the results.

    $result = mysql_query("
                SELECT * FROM wp_postmeta
                (meta_key= 'class' AND (meta_value = '$a2' OR meta_value = '$a3' OR meta_value = '$a4' OR meta_value = '$a5' OR meta_value = '$a6' OR meta_value = '$a7' OR meta_value = '$a8'))

    What this does is if a box is selected, passes a true parameter (1) for the option in the url for the selected option(s) (ex. if ‘Class A’ and ‘Class C’ are selected). This seems to work fine.

    However, when I try to search for the combination of another meta_key and it’s values using this new query…

    $result = mysql_query("
                SELECT * FROM wp_postmeta
                (meta_key= 'class' AND (meta_value = '$a2' OR meta_value = '$a3' OR meta_value = '$a4' OR meta_value = '$a5' OR meta_value = '$a6' OR meta_value = '$a7' OR meta_value = '$a8'))
                (meta_key= 'fuel' AND (meta_value = '$c2' OR meta_value = '$c3' OR meta_value = '$c4'))

    … it does not work anymore 🙁 Each meta_key statement will work independently, but not together using the AND operator. It seems right to me but it’s not working. Strangely though, if I change the AND to OR at line 5 it works, but does not return the results I want as it expands the search instead of narrows it.

    Any help is greatly appreciated. I am also willing to try other methods if anyone knows how to pull this off as I could be going about this all wrong. I’m a designer first recently turned programmer and my level of expertise is intermediate at best so please be kind.

  • The topic ‘WordPress Custom Search’ is closed to new replies.