保存帖子时Wordpress ACF [英] Wordpress acf when save post

查看:94
本文介绍了保存帖子时Wordpress ACF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我保存帖子时,它会创建一个具有正确值的帖子,而创建一个没有标题且没有值的帖子!

When I save the post It create one post with correct value and an other with no title and no value!

add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );

function tsm_do_pre_save_post( $post_id ) {


        // Create a new post
        $post = array(
            'post_type'     => 'itemfounds', // Your post type ( post, page, custom post type )
            'post_status'   => 'draft', // (publish, draft, private, etc.)
            'post_title'    => 'Δωρεά σε είδος για το "'.get_the_title(wp_strip_all_tags( $_POST['acf']['field_5696694332974'] )).'"' , // Post Title ACF field key 
        );

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

    session_start();

    $_SESSION['item_pid'] = $post_id;

    // Save the fields to the post
   // do_action( 'acf/save_post' , $post_id );


    return $post_id;

}


推荐答案

您可以通过以下方式使用acf:-

You can use acf by this way:-

$ post_id (数组)要保存的帖子的ID

$post_id (array) the ID of the post being saved

之前

<?php

function my_acf_save_post( $post_id ) {

    // bail early if no ACF data
    if( empty($_POST['acf']) ) {

        return;

    }


    // array of field values
    $fields = $_POST['acf'];


    // specific field value
    $field = $_POST['acf']['field_abc123'];

}

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

?>

之后

<?php

function my_acf_save_post( $post_id ) {

    // get new value
    $value = get_field('my_field');


    // do something

}

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

?>

希望这会帮助您:)

这篇关于保存帖子时Wordpress ACF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆