ACF在前端发布新帖子时遇到问题

时间:2019-01-23 12:25:46

标签: php wordpress

我将“高级自定义”字段用于自定义帖子类型,以允许用户从前端添加帖子。我的更新代码工作正常。插入新帖子无法正常工作。它只是添加了一个帖子,但没有保存任何数据。

我尝试了以下代码。

function my_acf_save_post( $post_id ) {
if( $post_id != 'new_post' ) {
    // Get the selected post status
    $value = get_field('post_status_field', $post_id);

    // Update current post
    $post = array(
      'ID'           => $post_id,
      'post_status'   => $value,    
      'post_title'  => $_POST['acf']['_post_title'],

    );

    // Remove the action to avoid infinite loop
    remove_action('acf/save_post', 'my_acf_save_post', 20);

    // Update the post into the database
    $post_id = wp_update_post( $post );


    // Add the action back
    do_action('acf/save_post', $post_id, 20);

}else{
    // Get the selected post status
    $value = get_field('post_status_field', $post_id);

    // Update current post
    $post = array(
      'ID'           => $post_id,
      'post_status'   => $value,    
      'post_title'  => $_POST['acf']['_post_title'],

    );

    // Remove the action to avoid infinite loop
    remove_action('acf/save_post', 'my_acf_save_post', 20);

    // Update the post into the database
    $post_id = wp_insert_post( $post );


    // Add the action back
    do_action('acf/save_post', $post_id, 20);


}
return $post_id;
}

// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);

我无法弄清楚哪里出了问题。我想在提交帖子时提交所有数据,并且在更新时必须更新所有数据。

1 个答案:

答案 0 :(得分:0)

有趣的是,我昨天解决了。 当您钩上'acf / save_post'时,您正在运行的函数会在自定义字段具有旧值的情况下通过$ _POST发送新值,而在get_field()中发送旧值时:D 所以

Person.ID    Household.ID    Composition 
   1             4593           1A_0C
   2             4992           2A_1C
   3             9843           1A_1C 
   4             8385           2A_2C  
   5             9823           8A_1C 
   6             3458           1C_9C 
   7             7485           2C_0C 
   :               :              :    

在两种情况下都需要使用,否则需要按两次更新按钮

Person.ID     Household.ID     Composition 
    5             9823            4+A_1C
    6             3458             1A_4+C
    :               :                :

获得FIELD_KEY做

$responseResult =
[
  [
    'subject' => 'one',
    'summary' => 'summary one',
  ],
  [
    'subject' => 'two',
    'summary' => 'summary two',
  ],
  [
    'subject' => 'three',
    'summary' => 'summary three',
  ],
];

error_clear_last();

$keyValuePairs = @array_combine
( array_slice(array_column($responseResult, 'subject'), 0, 50),
  array_slice(array_column($responseResult, 'summary'), 0, 50)
);

if(error_get_last())
  echo 'Error handling';
else
{
  echo "summary of subject two: {$keyValuePairs['two']} <br>\n";

  foreach ($keyValuePairs as $k => $v)
    echo "$k => $v <br>\n";
}