使用前端更新GEO我的WP字段高级自定义字段表单 - WordPress

时间:2017-07-14 10:33:49

标签: wordpress insert custom-post-type advanced-custom-fields geo

我使用高级自定义字段(ACF)插件创建了一个前端表单。在此表单中,我添加了一些位置字段,例如邮政编码和城市。

以下是用于更新帖子的函数

global $post_id;
add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );

function tsm_do_pre_save_post( $post_id ) {

 // Bail if not logged in or not able to post
 if ( ! ( is_user_logged_in() ) ) {
 return;
 }

 // check if this is to be a new post
 if( $post_id != 'new_job' ) {
 return $post_id;
}

$profile_id = um_profile_id();
$userID = 'user_'.$profile_id;

$user_id = get_current_user_id();

    // Create a new post
    $post = array(
       'ID'=> $post_id,
       'post_type' => 'members', 
       'post_status' => 'publish', 
       'post_title' => $userID, 
       'post_author' => $user_id,
       'category' => $_POST['acf']['field_594d0ffc2a66d'],
    );

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

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

 return $post_id;
     exit;
}

提交此表单后,自定义帖子类型会更新,不会出现任何问题。

如何将提交的位置数据(邮政编码,城市等)添加到帖子的GEO MY WP信息中?

我试图按照文档中的信息(http://docs.geomywp.com/gmw_pt_update_location/)进行操作,但没有太多运气。

GEO我的WP功能从下面的文档:

function gmw_update_post_type_post_location(  $post_id ) {

// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )
    return;

// check autosave //
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
}

//check if user can edit post
if ( !current_user_can( 'edit_post', $post_id ) )
    return;

//get the address from the custom field "address"
$address = get_post_meta( $post_id, 'address', true );

//varify that address exists. Otherwise abort the function.
if ( empty( $address ) )
    return;

//include the update location file file
include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );

//make sure the file included and the function exists
if ( !function_exists( 'gmw_pt_update_location' ) )
    return;

//Create the array that will pass to the function
$args = array(
        'post_id' => $post_id, //Post Id of the post 
        'address' => $address // the address we pull from the custom field above
);

//run the udpate location function
gmw_pt_update_location( $args );
}
//execute the function whenever post type is being updated
add_action( 'save_post_post', 'gmw_update_post_type_post_location' );

我相信我需要以某种方式组合这两个函数,所以当更新ACF表单时,它还需要更新GEO我的wp元数据。

我尝试过将这两个功能结合起来,但没有太多运气。

如果有人能指出我正确的方向,我们将非常感激

由于

更新代码.....

我试图在ACF函数中添加gmw_update_post_type_post_location()函数(下面的代码),但仍然没有太多运气......

global $post_id;
add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
function tsm_do_pre_save_post( $post_id ) {

 // Bail if not logged in or not able to post
 if ( ! ( is_user_logged_in() ) ) {
 return;
 }

 // check if this is to be a new post
 if( $post_id != 'new_job' ) {
 return $post_id;
 }

$profile_id = um_profile_id();
$userID = 'user_'.$profile_id;

$user_id = get_current_user_id();

$profilePostcode = get_post_meta( $post_id, 'location_postcode', true );

    // Create a new post
    $post = array(
       'ID'=> $post_id,
       'post_type' => 'members', // Your post type ( post, page, custom post type )
       'post_status' => 'publish', // You can use -> publish, draft, private, etc.
       'post_title' => $userID, // Post Title ACF field key
       'post_author' => $user_id,
       'category' => $_POST['acf']['field_594d0ffc2a66d'], // Post Content ACF field key
    );

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

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

gmw_update_post_type_members_location($post_id);

 return $post_id;

 exit;
}

function gmw_update_post_type_members_location(  $post_id ) {

    $profilePostcode = get_post_meta( $post_id, 'profile_location_postcode', true );

    if ( empty( $profilePostcode ) )
        return;


    $address = array(
        //'street'   => $_POST['location_address'],
        //'city'     => $_POST['location_town'],
        //'state'    => $_POST['location_state'],
        'zipcode'  => $profilePostcode,
        //'country'  => $_POST['location_country']
    ); 

    include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );

    if ( !function_exists( 'gmw_pt_update_location' ) )
        return;

    $profile_id = um_profile_id();
    $userID = 'user_'.$profile_id;

    $user_id = get_current_user_id();

    $args = array(
        'post_id' => $post_id, //Post Id of the post
        'post_title' => $userID, // Post Title ACF field key
        'post_author' => $user_id,
        'post_type' => 'members', 
        'address' => $address

    );

    //run the udpate location function
    gmw_pt_update_location( $args );            
}

0 个答案:

没有答案