最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

query posts custom field calculation value

programmeradmin0浏览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?

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?

Share Improve this question edited Oct 4, 2019 at 8:04 Pete asked Oct 4, 2019 at 7:21 PetePete 1,0582 gold badges14 silver badges40 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

}
发布评论

评论列表(0)

  1. 暂无评论