Title: write to custom field using sql statement
Last modified: August 19, 2016

---

# write to custom field using sql statement

 *  [everten2](https://wordpress.org/support/users/everten2/)
 * (@everten2)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/write-to-custom-field-using-sql-statement/)
 * Is it possible to write a value to a custom field using a sql statement during
   the loop or other means?
 * I have 5 custom fields that contain numbers. I want to calculate the average 
   of these numbers and write it back to the database into a 6th custom field.
 * Is this advisable? How will it affect performance of the website?
 * Thanks.
    Jeff

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/write-to-custom-field-using-sql-statement/#post-1399410)
 * Saw your other thread with a similar question. [http://wordpress.org/support/topic/366035?replies=1](http://wordpress.org/support/topic/366035?replies=1)
 * Guess the question is, what are you going to do with the data once it is saved?
   If the data is for display, then just calculate it on the fly.
 * If you have to save the data, then will need to use the `save_post` and `publish_post`
   filters, but that’s beyond me–look at a plugin such as More Fields for idea on
   how to handle that.
 * I’m going to close your other post and point it to this thread.
 *  Thread Starter [everten2](https://wordpress.org/support/users/everten2/)
 * (@everten2)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/write-to-custom-field-using-sql-statement/#post-1399459)
 * I want to sort by the average. I have already been able to calculate and display
   the average on the fly by doing the following in the loop:
 * <?php
    $rating1 = get(‘customfield1’); $rating2 = get(‘customfield2’); $rating3
   = get(‘customfield3’); $rating4 = get(‘customfield4’); $rating5 = get(‘customfield5’);
 *  $totalRatings = $rating1+$rating2+$rating3+$rating4+$rating5;
    $totalVotes =
   5; $average = $totalRatings / $totalVotes; ?>
 * My ultimate objective is to be able to sort posts based on the the average.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘write to custom field using sql statement’ is closed to new replies.

## Tags

 * [average](https://wordpress.org/support/topic-tag/average/)
 * [calculate](https://wordpress.org/support/topic-tag/calculate/)
 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [sort](https://wordpress.org/support/topic-tag/sort/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [everten2](https://wordpress.org/support/users/everten2/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/write-to-custom-field-using-sql-statement/#post-1399459)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
