Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

php - Wordpress include all meta values in search if blank field

Okay so I'm creating a custom search for a client that takes drop-down input and then shows all the products that meet that criteria.

I've got it working in that if you put in information for a specific product, it will return just that product in the results. I want it to do exactly what it does, but I also want it to return partial matches if drop-downs aren't selected.

Right now if you select two of the four drop downs, it doesn't return any results. I want it to return results based on what was selected, no matter which drop down or how many are selected.

Example is here: http://carotape.bigwolfdesigns.com/product-selector/

code is here:

<?if ( !$_GET['submit'] ) : ?>

<form method="get">
    <?php
        echo "<br><strong>Thickness:</strong><br>";
        product_selector('milThickness_box', 'thickness');
        echo "<br><strong>Adhesion:</strong><br>";
        product_selector('adhesion_box', 'adhesion');
        echo "<br><strong>Substrate:</strong><br>";
        product_selector('substrate_box', 'substrate');
        echo "<br><strong>Elongation:</strong><br>";
        product_selector('elongation_box', 'elongation');
    ?>
    <br>
    <input type="submit" name="submit" value="submit">
    </form>
        <?php else : 
        $ad = array( 'key' => 'adhesion_box', 'value' => $_GET['adhesion'] );
        $el = array( 'key' => 'elongation_box', 'value' => $_GET['elongation'] );
        $th = array( 'key' => 'milThickness_box', 'value' => $_GET['thickness'] );
        $su = array( 'key' => 'substrate_box', 'value' => $_GET['substrate'] );
        $args = array(
        'post_type' => 'post',
        'meta_query' => array($ad, $el, $th, $su),
    );
    $myQuery = new WP_Query($args); ?>
        <ul>
         <?php 
         while ($myQuery->have_posts()) : 
         $myQuery->the_post(); ?>
              <li><a href="<?php the_permalink(); ?>">
                   <?php the_title(); ?></a>
              </li>
         <?php endwhile; ?>
    </ul>
        <?php endif; ?>

The product_selector(); function simply takes the input variable and uses it to read the specific meta_key into drop downs with foreach().

function product_selector($var, $n) {
    //Declare Globals
        global $wpdb;
    // set the meta_key to the appropriate custom field meta key
    $a = $wpdb->get_col( $wpdb->prepare( 
        "
            SELECT meta_value 
            FROM $wpdb->postmeta 
            WHERE meta_key = %s
        ",     
        $var
    ) ); 

    //Remove duplicate values.
    $b = array_unique($a);
    //Remove null values.
    $c = array_filter($b);
    //Sort the array
    $d = sort($c);


    // Print our form.
    echo "<select name="$n">";
        echo "<option value="">Please Choose</option>";
    foreach ( $c as $k => $v ) :
        echo "<option value="$v">$v</option>";
    endforeach;
    echo "</select>"; 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First you have to check whether $_GET[] is empty or not then assign the values to an array.

if( !empty($_GET['adhesion']) {
     $ad = array( 'key' => 'adhesion_box', 'value' => $_GET['adhesion'] );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...