WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

At the moment i have a custom field named "price" to order my posts correctly.

The following already works:

  $query_args['meta_key'] = 'price';
  $query_args['orderby'] = 'meta_value_num';
  $query_args['order'] = 'asc';

The problem is that this is a "recommended" price, the real price needs to be calculated inside the loop.

The question is to know if there is a way or a trick, where we can run the loop, calculate the real price and show the posts based on the real price variable.

share|improve this question
How do you (want to) calculate the "real price"? That question aside, you will probably have to go through an additional iteration. – Johannes Pille Mar 16 '13 at 17:59
the price needs to be calculated within the loop, it receives two variables by GET method, Arrival and Departure date. I in each user search i save a custom field probably it will overload the server or not? – Barbio Mar 18 '13 at 7:32

At the moment you publish/update the post with recommended price, it's better to have another custom meta post to save the real price in it by using add_action() and add_post_meta(), then you can use the query you posted but with $query_args['meta_key'] = 'real_price';

Something like below:

function wp_po54785( $post_id )
    $recommended_price = get_post_meta( $post_id, 'price', true );
    if ( ! $recommended_price )

    // Avoid infinite loops
    remove_action( current_filter(), __FUNCTION__ );
    // If you're doing this from inside a class:
    # remove_action( current_filter(), array( $this, __FUNCTION__ ) );

    // The Algorithm
    // You use to
    // Calculate real price
    // By doing works on $recommended_price
    // And put in $real_price;
    add_post_meta( $post_id, 'real_price', $real_price );
add_action( 'save_post', 'wp_po54785' );
share|improve this answer
If it is possible to "pre-calculate", then yes this would be the way to go. I am not sure why the OP stated that "the real price needs to be calculated inside the loop" – s_ha_dum Mar 16 '13 at 22:03
@s_ha_dum what i said is an idea, what he said as I think is because he needs post id and access to the meta key value by it to do the calculation and echo-ing result inside the loop. – revo Mar 16 '13 at 22:05
Not arguing (I even upvoted), just stating a caveat. The OP does not state why the calculation needs to be done in the Loop so I am not sure what your interpretation is based on (but may be correct nonetheless). But if "real price" is based on something like currency exchange rates, for example, "pre-calculating" wouldn't be possible. – s_ha_dum Mar 16 '13 at 22:10
@s_ha_dum tnx. I'm not arguing too, man :) he should come & clarify. – revo Mar 16 '13 at 22:12
Updated your answer. There's another caveat. As long as you didn't save the post, you won't have a price. So you'd need to save each post twice to get the calculation done. Another possibility would be to not only use save_post, but as well one of the post display actions or similar to update this value without the need for a second save process. It's also important to note that you need to remove the action again, as you'd else run into an infinite loop in various scenarios where you use functions that trigger that hook again. – kaiser Mar 17 '13 at 11:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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