Rumen Panchev
Rumen Panchev

Reputation: 498

How to add column to wp_post table in database?

I have a database named event-listing-db which contains a table wp_posts. Also i have a function for creating a form with method post with the following structure:

function ru_meta_callback() {
    wp_nonce_field(basename(__FILE__), 'ru_jobs_nonce ');
    ?>
    <form method="POST">
        <div>
            <div class="meta-row">
                <div class="meta-th">
                    <label for="date_listed" class="ru-row-title">Event Date</label>
                </div>
                <div class="meta-td">
                    <input type="text" name="event_date" id="event_date" value=""/>
                </div>
            </div>
            <div class="meta-row">
                <div class="meta-th">
                    <label for="event_location" class="ru-row-title">Event Location</label>
                </div>
                <div class="meta-td">
                    <input type="text" name="event_location" id="event_location" value=""/>
                </div>
            </div>
            <input type="submit" value="Add Event">
        </div>
    </form>
    <?php
}

When I submit the form only the post_title column is filling. My question is: How can I create columns for event_date and event_location and fill'em when the data is submitted and save the new columns along with the original post?

Upvotes: 1

Views: 3549

Answers (2)

Pritpal Singh
Pritpal Singh

Reputation: 72

You don't have to add a new column to the wp_posts table. But you can add a new meta for the post. If you want to add the field in the admin panel for the new field you want. You can use the add_meta_box to add a new meta box for post edit screen and then use the update_post_meta on the save_post hook. Hope that helps.

The meta fields can be used in the WP_Query in the meta_query index. But you can't use the custom field added to the wp_posts table to filter or search for the posts with the new meta added to the posts.

Upvotes: 1

manian
manian

Reputation: 1438

You have few options to have additional data to your post,
-> You can use hooks to add additional fields like here, WordPress - Add extra column to wp_posts, and post to it
-> You can make use of custom fields to add additional data to your post. And there is a good plugin too for this, https://wordpress.org/plugins/advanced-custom-fields/.
-> You can use the wp_postmeta table too to save your additional data for your post

Upvotes: 1

Related Questions