1

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.

2
  • 1
    How do you (want to) calculate the "real price"? That question aside, you will probably have to go through an additional iteration. Commented Mar 16, 2013 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
    Commented Mar 18, 2013 at 7:32

1 Answer 1

3

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 )
        return;

    // 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' );
6
  • 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
    Commented Mar 16, 2013 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
    Commented Mar 16, 2013 at 22:05
  • 1
    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
    Commented Mar 16, 2013 at 22:10
  • @s_ha_dum tnx. I'm not arguing too, man :) he should come & clarify.
    – revo
    Commented Mar 16, 2013 at 22:12
  • 1
    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
    Commented Mar 17, 2013 at 11:29

Your Answer

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

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