drau89457 2014-07-16 17:06
浏览 45
已采纳

从前端表单更新自定义帖子元

This is how i am getting custom post meta within query.

<?php 
        $collections = get_post_meta( $post->ID, 'do_collection');
        if( !$collections || !$collections[0] )
            return;
            foreach ( $collections[0] as $collection ) :
            echo '<p>'.$collection['song-title'].'</p>';
            echo '<p>'.$collection['song-category'].'</p>';
            echo '<p>'.$collection['song-layrics'].'</p>';
        endforeach;
?>

Now i am trying to get custom post meta values from the form on frontend. but when i submit form from frontend i get all meta fields with a first alphabet in admin area.

<?php 
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

    $new_post = array(
    'post_status'   =>  'publish',
    'post_type' =>  'do_songs'
    );
    $pid = wp_insert_post($new_post);
    update_post_meta($pid, 'do_collection',  $_POST['song-title']);
    update_post_meta($pid, 'do_collection',  $_POST['song-category']);
    update_post_meta($pid, 'do_collection',  $_POST['song-layrics']);

    $link = get_permalink( $pid );
    wp_redirect( $link );
}
do_action('wp_insert_post', 'wp_insert_post');
?>

<div class="form-wrap">
<form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
    <fieldset>
        <label for="song-title">Song Title:</label>
        <input type="text" value="" tabindex="35" name="song-title" id="song-title" />
    </fieldset>
    <fieldset>
        <label for="song-category">Song Category:</label>
        <input type="text" value="" tabindex="35" name="song-category" id="song-category" />
    </fieldset>
    <fieldset>
        <label for="song-layrics">Song Layrics:</label>
        <input type="text" value="" tabindex="35" name="song-layrics" id="song-layrics" />
    </fieldset>
    <fieldset class="submit">
        <input type="submit" value="Post Review" tabindex="40" id="submit" name="submit" />
    </fieldset>
    <input type="hidden" name="action" value="new_post" />
    <?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>
  • 写回答

1条回答 默认 最新

  • duanji2002 2014-07-16 17:16
    关注

    Use this instead

    $collection = array();
    $collection['song-title'] = $_POST['song-title'];
    $collection['song-category'] = $_POST['song-category'];
    $collection['song-lyrics'] = $_POST['song-lyrics'];
    
    update_post_meta($pid, 'do_collection', $collection);
    

    Is the meta 'do_collection' supposed to be a multi-dimensional array? If not, you could update the way you retrieve the metadata:

    $collection = get_post_meta( $post->ID, 'do_collection', TRUE);
    
    if( !$collection ) return;
    
    echo '<p>'.$collection['song-title'].'</p>';
    echo '<p>'.$collection['song-category'].'</p>';
    echo '<p>'.$collection['song-layrics'].'</p>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程