Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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.

Metaboxes

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() {
    add_meta_box( 
        'marker_metabox',
        __('Marker Title', 'your-plugin-textdomain'),
        'marker_metabox',
        'post' 
    );
}

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'); ?>
    </label>
    <input type="text" id="marker_field" name="marker_field" value="<?php echo $marker; ?>" size="25" />
    <?php
}

And then, for saving it:

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

    /* 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.

share|improve this answer
    
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 ? –  savunit Aug 14 '12 at 0:19
    
I'm sorry, you should use add_meta_box in a hook callback. I just edited the snippet, take a look. –  vmassuchetto Aug 14 '12 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 –  savunit Aug 14 '12 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. –  vmassuchetto Aug 14 '12 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. –  savunit Aug 17 '12 at 19:35
show 3 more comments

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.

    http://codex.wordpress.org/Custom_Fields

  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.

    http://codex.wordpress.org/Class_Reference/wpdb

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

share|improve this answer
    
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 at 18:20
add comment

Your Answer

 
discard

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.