At the moment I'm using a custom post type called 'locations' I would like to be able to put input fields into the custom post type that would store the information such as ( address, type, phone number, etc.. ) in a separate table called 'markers' So when a new post is made it will create a new entry in the table markers from the following input fields / have it updatable or removable when post is either changed or deleted.

I'm not sure where to start to get these linked together to work in such a fashion, I know how to make a insert.php file to add new locations and to create a database but not with wordpress in such a fashion. My apologies but I'm pretty new to SQL and PHP.

3 Answers 3


You should avoid creating more tables. Just do it if you really have a good reason to. Note that wp_postmeta can store practically any kind of data, and simply using the get_post_meta function can do all the job in most cases.

But if you must use another table, and taking it generally, then you're looking for the save_post and delete_post hooks.

Replicating custom fields

Because you mention custom fields, if you want to replicate the information from wp_postmeta to another table, you would do something like:

add_action('save_post', 'save_markers');
function save_markers($post_id) {
    global $wpdb;
    $marker = get_post_meta($post_id, 'marker_custom_field', true);
    // go and put $marker on the markers table
    $wpdb->query("INSERT INTO markers ...");

The above applies also for the delete_post hook.


But, if you want to store the information directly on the other table, maybe metaboxes suits you better.

add_action( 'add_meta_boxes', 'marker_add_meta_box');
function marker_add_meta_box() {
        __('Marker Title', 'your-plugin-textdomain'),

function marker_metabox ($post) {
    wp_nonce_field(plugin_basename( __FILE__ ), 'your-plugin-texdomain');
    $marker = get_marker_from_another_table();
    <label for="marker_field">
        <?php _e('Marker field description', 'your-plugin-texdomain'); ?>
    <input type="text" id="marker_field" name="marker_field" value="<?php echo $marker; ?>" size="25" />

And then, for saving it:

add_action('save_post', 'save_markers');
function save_markers ($post_id) {
    if (defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) 
    if ('locations' != $_POST['post_type'])
    if (!current_user_can( 'edit_post', $post_id ))

    /* Here you have $marker and $post_id, then you can just
       do the reference in your markers table. */

    global $wpdb;

    $marker = $_POST['marker_field'];
    $wpdb->query("INSERT INTO markers VALUES ...");


And of course, you can do more verifications with the code above.

  • Having a quick issue with it, I get this response. Fatal error: Call to undefined function add_meta_box() in /home/wordpress/public_html/wp/wp-content/themes/wordpress/functions.php on line 215 How would I go about fixing this ? Aug 14, 2012 at 0:19
  • I'm sorry, you should use add_meta_box in a hook callback. I just edited the snippet, take a look. Aug 14, 2012 at 1:27
  • One last error I'm getting, sorry for needing so much help! Fatal error: Call to undefined function get_marker_from_another_table() in /home/wordpress/public_html/wp/wp-content/themes/wordpress/functions.php on line 227 Aug 14, 2012 at 1:37
  • Oh... I can't help you with this one. =D That's a function call I just put there to describe what you have to do. Please, don't just copy and paste the snippets beucase they won't work, use them as a reference for your own development. Aug 14, 2012 at 1:43
  • I have a quick question, thanks for all your help. I have most of it figured out besides for if I use the add and delete hook to replicate the information from the posts_meta db onto the 'markers' db how would I also have it update? That's the last part I'm stuck on. Aug 17, 2012 at 19:35

You have 3 options.

  1. Just use the native custom fields in WordPress, unless you have a good reason not to, they would suit you fine and work perfectly with custom post types.


  2. Create your own custom fields and tie them into your CPT using wpdb. You need a good reason to do this instead of using option1.


  3. Check out the pods framework, it is basically CPT's with more control over the DB.

  • 1
    is performance a good reason to go for option 2? let's say I have 10 fields and I want to display all 10 fields in a listing of posts... i need to do 10 JOINS with metaposts right?
    – yeahman
    Apr 9, 2014 at 18:20
  • 1
    yes @yeahman, performance is a major reason for option 2. displaying 10 fields in a listing of posts wont be a problem, but SEARCHING and selecting such posts will be. querying 10 metas from wp postmeta will be slow as hell.
    – unity100
    Jan 22, 2016 at 21:28
INSERT INTO `wp_postmeta` (post_id, meta_key, meta_value) VALUES (*ID*, 'translation', *VALUE*;

Where ID is the id of the post you're attaching the value for and VALUE is the meta value of the "translation" field.

Like I said, doable ... but you'll need a separate INSERT query for each post. You could dump all of these into a single text file and run the entire set in one pass if you want, otherwise it might take as much time as it would to just add the key through the WordPress UI in the first place.

  • 1
    Welcome to Wordpress SE! In the future, could you please post your answer in one go, instead of first posting garbage? This triggers spam detection algorithms, which might cause your post to be deleted.
    – Glorfindel
    Jan 28, 2017 at 12:16

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.