【问题标题】:update custom post meta from front end form从前端表单更新自定义帖子元
【发布时间】:2014-07-16 17:06:21
【问题描述】:

这就是我在查询中获取自定义帖子元数据的方式。

<?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;
?>

现在我正在尝试从前端的表单中获取自定义帖子元值。 但是当我从前端提交表单时,我会在管理区域中获得所有带有第一个字母的元字段。

<?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>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    改用这个

    $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);
    

    元“do_collection”是否应该是一个多维数组? 如果没有,您可以更新检索元数据的方式:

    $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>';
    

    【讨论】:

    • 请务必转义您的 $_POST 数据。 codex.wordpress.org/…
    • 感谢您的回复,但使用您的解决方案后仍然出现同样的问题。如果我在歌曲标题字段中写“TEST”,我会在所有字段中得到“T”。
    • 元 'do_collection' 应该是一个多维数组吗?如果没有,您可以更新检索元数据的方式: $collection = get_post_meta( $post->ID, 'do_collection', TRUE); if( !$collections ) 返回; echo '

      '.$collection['song-title'].'

      '; echo '

      '.$collection['song-category'].'

      '; echo '

      '.$collection['song-layrics'].'

      ';
    • 谢谢你的回答是可以接受的。我做了 $collection[0]['song-title'] = $_POST['song-title'];并且它修复了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    相关资源
    最近更新 更多