从前端更新wordpress帖子(使用acf)

时间:2014-01-10 12:17:23

标签: wordpress advanced-custom-fields

我正在为用户添加/编辑他们的帖子制作前端管理员。我已经发布了添加表单并且它可以工作,但编辑表单不起作用。

的functions.php

function add_new_post( $post_id )
{
    if( $post_id == 'new' ) {
        // Create a new post
        $post = array(
            'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
            'post_category' => array(4),
            'post_status'  => 'draft',
            'post_type'  => 'post'
        );

        // insert the post
        $post_id = wp_insert_post( $post );

        return $post_id;
    }
    else {
        return $post_id;
    }
}add_filter('acf/pre_save_post' , 'add_new_post' );

的index.php

<div id="updateform-<?php the_ID(); ?>" class="collapse">
    <?php
    echo get_the_ID();
    $args = array(
        'post_id' => get_the_ID(), // post id to get field groups from and save data to
        'field_groups' => array(31), // this will find the field groups for this post (post ID's of the acf post objects)
        'form' => true, // set this to false to prevent the <form> tag from being created
        'form_attributes' => array( // attributes will be added to the form element
            'id' => 'post',
            'class' => '',
            'action' => get_permalink( get_the_ID() ),
            'method' => 'post',
        ),
        'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
        'html_before_fields' => '', // html inside form before fields
        'html_after_fields' => '', // html inside form after fields
        'submit_value' => 'Update', // value for submit field
        'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
    );
    acf_form( $args );
    ?>
</div>

3 个答案:

答案 0 :(得分:1)

要保存帖子,您应使用save_post而不是pre_save_post。 基本上pre_save_post用于新帖子。

使用以下add_filter('save_post' , 'add_new_post');

答案 1 :(得分:0)

我为此创建了插件:

  1. 表示行动 https://wordpress.org/plugins/forms-actions/
  2. 向您的ACF表单添加操作。

    1. ACF前端显示 https://wordpress.org/plugins/acf-frontend-display/
    2. 在前端显示您的ACF表格

答案 2 :(得分:0)

如果您碰巧正巧使用Elementor Page Builder,则可以通过安装Advanced Widgets for Elementor来实现。它带有ACF Form小部件,您可以将其拖放到站点中的任何位置并通过UI配置表单(无需编码)。

相关问题