0

I have a front-end form whereby users create a post and input 3 numerical custom field value` as part of their post.

These 3 custom fields are then calculated and the result is displayed in the post.

Is there a way I can query/list/display the posts according to the calculated result/value?

3

Maybe with SQL, but I wouldnt suggest it, so you have two options:

Query a full list, and then check your condition on each post retrieved.

or

Save the product of those three fields in another meta field, which can then be queried.

EDIT: For the second method, it really depends on the context. if you are handling the saving via wp_insert_post, then you can save this extra meta field via update_post_meta.

if you are not handling the saving on the server, you can hook to the saving via save_post hook, and then use get_post_meta to retrieve the values of each field, and then update_post_meta to save the product on a field of your choice, for example

add_action( 'save_post', 'add_product_meta' ); //triggers for every post saved

function add_product_meta ( $post_id )
{
  $value1 = get_post_meta( $post_id, 'value1_key', true );
  $value2 = get_post_meta( $post_id, 'value2_key', true );
  $value3 = get_post_meta( $post_id, 'value3_key', true );

  $product = $value1 * $value2 * $value3;
  update_post_meta( $prod_id, 'product_of_values', $product );
  //use 'product_of_values' as your meta_key for the query

}
4
  • Could you elaborate on your second method please. – Pete Oct 4 '19 at 8:06
  • Would this example be something I could use? toolset.com/forums/topic/… – Pete Oct 4 '19 at 8:09
  • Yes, kind of, but the question is when to do it, because we dont know your setup. – Jess_Pinkman Oct 4 '19 at 8:12
  • It is a simple front-end form creating a post. I don't care when the new custom field (with the calculation) is created, just as long as it is. – Pete Oct 4 '19 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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