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

I have a function that performs some custom queries to get specific information regarding a post (children count, dates, etc).

I am wondering if it is possible to send this custom query info (set as a variable) to a custom field automatically when I update the page.

share|improve this question
up vote 0 down vote accepted

Sure. Hook into the save_post action, then perform your custom queries upon save, and store them as custom fields of the post.

add_action('save_post', 'my_extra_meta_saver');
function my_extra_meta_saver($post_id)
    global $post;

    // Prepare whatever extra information you want here

    // Save each piece of info in a custom field
    update_post_meta($post_id, 'meta_key', 'meta_value');
share|improve this answer
Does the add_action have to go in a plugin or can it be placed in the functions file as well, outside of the function? – AlxVallejo Nov 21 '11 at 20:02
It can go in functions.php. – Geert Nov 21 '11 at 20:49

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.