WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Multiple meta values, one form submission (10 posts)

  1. Jon Bourne
    Member
    Posted 7 years ago #

    How can I insert a new post with multiple metakeys and metavalues all in one step, ie. from the same page/form?

  2. Jon Bourne
    Member
    Posted 7 years ago #

    I figured it out myself. In case anyone is interested, here is the relevant code.

    <?php

    include('./wp-config.php');
    include('./wp-admin/admin-functions.php');

    $post_id = 4;

    $wp_meta = array(
    'test-01' => 'test-01 value',
    'test-02' => 'test-02 value',
    'test-03' => 'test-03 value',
    'test-04' => 'test-04 value',
    'test-05' => 'test-05 value',
    'test-06' => 'test-06 value',
    'test-07' => 'test-07 value',
    'test-08' => 'test-08 value',
    'test-09' => 'test-09 value'
    );

    foreach($wp_meta as $metakey=>$metavalue) {
    $_POST = array();
    $_POST['metakeyinput'] = $metakey;
    $_POST['metavalue'] = $metavalue;
    add_meta($post_id);
    }

    ?>

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    This line: $_POST['metakeyselect'] = '#NONE#'; is actually unnecessary. It's faster without it, although the difference will be so minimal that I doubt you'd ever notice. Still... ;)

  4. Jon Bourne
    Member
    Posted 7 years ago #

    Thanks. I've edited it out of the above code.

  5. Dgold
    Member
    Posted 7 years ago #

    I don't understand the code above, but... another option for this could be the Custom Fields GUI plugin.

    It lets you pre-put Key/Value metas on the Write Post page, so it's already there when you show up to write a new post.

  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    DGold: Why would you need a plugin for that? Can't you do that already on the Write Post page? At the very bottom, there's a Custom Fields expandable bar.

  7. Jon Bourne
    Member
    Posted 7 years ago #

    Also, a GUI wouldn't help in this case because I am importing posts from static XML files that are uploaded each night. I am not using a GUI. Instead, I am accessing WP's functions directly via a PHP script that parses all the files and passes the WP functions paraters as though I were submitting them through a form, ie. in the $_POST array. No GUI ever comes into play.

  8. Jon Bourne
    Member
    Posted 7 years ago #

    Obviously, test-01 and test-01 value are not the actual keys and values I am storing. For the record, the real data that I am putting in the meta fields for each post are things like subheadlines, bylines for unrecognized authors, information boxes associated with stories, and pull quotes.

  9. logista
    Member
    Posted 7 years ago #

    Why would you need a plugin for that? Can't you do that already on the Write Post page? At the very bottom, there's a Custom Fields expandable bar.

    I have 4 custom fields on nearly every post. It's a big pain in my neck to click the dropdown (which *always* changes order), enter my data, click update, wait for the save, click the dropdown, enter data.... the Custom Field GUI lets you enter the fields all in one fell swoop.

  10. Dgold
    Member
    Posted 7 years ago #

    @Otto42, I don't know whether you or akbigdog would need that plugin (apparently not).

    However it is an extremely useful plugin, because it saves time and reduces confusion for novice writers on my sites. It saves time because the 5 or 6 custom field keys that I use regularly are there with my choice of radio buttons, check boxes, or fill-in-the-blank for the value (instead of having to manually type the question/key and answer/value each time, I simply press a radio button). For reducing confusion, this looks a lot more "normal" for the internet as far as a writer (non-WP-expert) posting on my site: they see the obvious radio buttons that they must choose before Publishing, instead of something that looks very techy at the bottom -- people don't understand what a "custom field key" is, but they easily understand a check-list that says (for example):
    What's Your Mood? Check One! A. Happy, B. Sad, C. Gleeful.

    Therefore it is one of my favorite plugins.

Topic Closed

This topic has been closed to new replies.

About this Topic