0

What I am trying to do is query a WordPress custom post type using meta keys from a search form. The search form takes user inputs and show the results based on the matching criteria, Some of the form's fields might be blank, so I need to make sure I don't pass any blank value to the query argument. I need to use if within the arguments array.

I'll be grateful for any type of help.

This is the code I am using, but getting an error message.

Parse error: syntax error, unexpected T_IF, expecting ')'

Here is my code:

        if (isset($POST['stock']) && !empty($POST['stock'])) {
            $stock = $POST['stock'];
        }
        if (isset($POST['mineral']) && !empty($POST['mineral'])) {
            $mineral = $POST['mineral'];
        }
        if (isset($POST['species']) && !empty($POST['species'])) {
            $species = $POST['species'];
        }
        if (isset($POST['color']) && !empty($POST['color'])) {
            $color = $POST['color'];
        }
        if (isset($POST['chemicalclass']) && !empty($POST['chemicalclass'])) {
            $chemicalclass = $POST['chemicalclass'];
        }
        if (isset($POST['locality']) && !empty($POST['locality'])) {
            $locality = $POST['locality'];
        }
        if (isset($POST['description']) && !empty($POST['description'])) {
            $description = $POST['description'];
        }
        if (isset($POST['size']) && !empty($POST['size'])) {
            $size = $POST['size'];
        }
        if (isset($POST['pricegt'])) {
            $pricegt = $POST['pricegt'];
        } else {
            $pricegt = 0;
        }
        if (isset($POST['pricelt'])) {
            $pricelt = $POST['pricelt'];
        } else {
            $pricelt = 999999;
        }

        $args = array(
            'post_type'         => 'products',
            'productspecies'    => $species,
            'localities'        => $locality,
            'meta_query' => array(
                //'relation' => 'OR',
                array(
                    'key' => '_price',
                    'value' => array( $pricegt, $pricelt ),
                    'compare' => 'BETWEEN',
                    'type' => 'numeric',
                ),
                if ($mineral) {
                array(
                    'key' => '_components',
                    'value' => $mineral,
                ),
                }
                if ($stock) {
                array(
                    'key' => '_lotnum',
                    'value' => $stock,
                ),
                }
                if ($color) {
                array(
                    'key' => '_color',
                    'value' => $color,
                ),
                }
                if ($description) {
                array(
                    'key' => '_smalldesc',
                    'value' => $description,
                    'compare' => 'LIKE',
                ),
                }
                if ($size) {
                array(
                    'key' => '_size',
                    'value' => $size,
                ),
                }
                if ($chemicalclass) {
                array(
                    'key' => '_chemicalclass',
                    'valeu' => $chemicalclass,
                ),
                }
            ),
            );
    ?>
        <?php $query = new WP_Query( $args ); ?>

        <div class="postcount">We Found A Total Of <span><?php echo $query->post_count;?></span> Items Maching Your Search</div>

    <?php if ( $query->have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>

What I am doing wrong?

5
  • did you used these if inside the array? Commented Jul 19, 2013 at 13:58
  • Which line? Also try removing the last , from the ) just before the <?php $query = new WP_Query( $args ); ?> Commented Jul 19, 2013 at 14:00
  • 4
    Off-topic: shouldn't $POST be $_POST? Commented Jul 19, 2013 at 14:01
  • @AndriusNaruševičius We can't be sure if he is using a $POST variable, but you're probably right. Commented Jul 19, 2013 at 14:02
  • @AndriusNaruševičius yes, it should be $_POST. I am sorry. Commented Jul 19, 2013 at 14:36

3 Answers 3

5

You are trying to pass if statements as arguments to the array() function. PHP does not allow that. One thing you can do is build the array without the optional parts and then add them later if necessary:

if ($stock) {
    $args['metaquery'][] = array(
        'key' => '_lotnum',
        'value' => $stock
    );
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can't insert instructions in the array initialization code.

Do it this way:

$args = array();

if (something){
    $args['metaquery'][] = array(contentsOfTheInnerArray);
}
if (something2){
    $args['metaquery'][] = array(contentsOfTheInnerArray2);
}

Comments

0

Not completely sure if this works (in php that is) but you can do something like this:

$array = array(
    'price' => isset($POST['pricegt']) ? $POST['pricegt'] : 0,
    ...
);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.