• I want to get all items where ‘price – ((discount/100)*price)’ is between two values.
    How can I do this?
    My code:
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘price – ((discount/100)*price)’,
    ‘value’ => array($precio_min, $precio_max),
    ‘compare’ => ‘BETWEEN’,
    ‘type’ => ‘NUMERIC’
    )
    );
    Regards!
    I want than all ‘price – ((discount/100)*price)’ BETWEEN two vaules, but key only accepts a custom field not an expresion

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    create a new variable for store numeric operation.

    for example,

    $temp = ((discount/100)*price);
    $key = 'price-'.$temp;

    Then you can assign $key varible in key of Meta Query.

    Full Solution :
    ————————

    $temp = ((discount/100)*price);
    $key = 'price-'.$temp;
    
    'meta_query' => array(
      array(
       'key' => $key,
       'value' => array($precio_min, $precio_max),
       'compare' => 'BETWEEN',
       'type' => 'NUMERIC'
      )
    );

    [Moderator Note: Code fixed. To ensure your code is usable by others, always enclose your code in `backticks`, or use the ‘code’ button.]

    • This reply was modified 7 years, 1 month ago by James Huff.
    • This reply was modified 7 years, 1 month ago by bcworkz.
    Thread Starter rocio480

    (@rocio480)

    Thank you. It works perfectly.
    I have troubles now trying to order the results.
    Could you help me?
    I have this var:
    $price_with_discount = (price – ((discount/100)*price ); ->The price of the car with a discount.
    I need to get the car’s price with a commission:
    $commission=1.33/100;
    $fee = (($price_with_discount*$commission)+$price_with_discount)/120;
    The idea is: select a car order by $fee.
    I tried:
    $args_min = array(
    ‘numberposts’ => 1,
    ‘post_type’ => ‘ficha_vehiculo’,
    ‘post_status’ => ‘publish’,
    ‘orderby’ => $fee,
    ‘order’=>’ASC’,
    ‘meta_key’=>’price’
    );
    Can I do that?
    Also I ask you, should I select “meta_key”=’price’. I don’t know if this is also ok…
    Regards

    Hi,

    orderby => $fee
    This is wrong.

    We can’t do order by like this.
    We can orderby only on that values which is saved in the Database.

    1st way
    Fetch the data in the simple way, and store in the Array.
    And we can change the the order as per our requirements.

    2nd way

    add_action( 'transition_post_status', 'wp_status_publish', 10, 3 );
    
    function wp_status_publish( $new_status, $old_status, $post ) { 
        if ( $new_status == 'publish' && $old_status == 'auto-draft' ) {
            $id = $post->ID;
    	$type = get_post_type( $id );
    	$meta_check = get_post_meta($id, 'commission_added', true);
    
    	if( $type == 'ficha_vehiculo' && $meta_check != "yes"){
    	    $price = get_field('price');
    			
    	  // Your Calculations
    	    $temp = ((discount/100)*$price);
                $discount = $price-$temp;
    
    	    $commission=1.33/100;
    	    $fee = (($discount*$commission)+$discount)/120;
    
    	    update_post_meta( $id, 'commission_added', 'yes', true );
    
    	    add_post_meta( $id, ''commission', $commission, true );
            }
    	update_post_meta( $id, 'commission_added', 'yes', true );
        }
    }
    

    Above code is executed when we published the New Post from Auto-Draft.
    And also above code is add the meta of Calculated Commission in current Post.
    For this we use “transition_post_status” Hook.

    With this you can get all the details related to post using Post ID.

    • Fetch the Price
    • Calculate the commission on it
    • Add the meta of that commission in the current Post ID.
      add_post_meta( $post_id, $meta_key, $meta_value, true );
      

    Now, You can fetch the data with OrderBy on Commission(Meta,which is added in above steps).

    2nd way is better

    And Sorry for late reply.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_query and a key with mathematical operations’ is closed to new replies.