PHPSOFT SOLUTIONS LOGO
  • Home
  • About Us
  • Contact Us
  • Contact Us
  • Blog
  • Online Courses
  • Contact US

wordpress insert post data and custom fields

Simple 3 steps:
1. create post basic values array of data

$my_post = array(
'post_title' => $_SESSION['booking-form-title'],
'post_date' => $_SESSION['cal_startdate'],
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'booking',
);

2 insert post data and get returned post id
$the_post_id = wp_insert_post( $my_post );
3. use same returned post id to add customfields value.


function __update_post_meta( $post_id, $field_name, $value = '' )
{
if ( empty( $value ) OR ! $value )
{
delete_post_meta( $post_id, $field_name );
}
elseif ( ! get_post_meta( $post_id, $field_name ) )
{
add_post_meta( $post_id, $field_name, $value );
}
else
{
update_post_meta( $post_id, $field_name, $value );
}
}

__update_post_meta( $the_post_id, 'my-custom-field', 'my_custom_field_value' );

Posted on March 2014
Category: Wordpress

Categories

  • Blog
  • phonegap
  • PHP Frameworks
  • php-jquery
  • system
  • Wordpress
  • Yii

Post navigation

Previous post

how to use ajax in wordpress

Next post

How to implement Google Sign in API on the website


PHPSOFT SOLUTIONS LOGO

Our mission is to satisfy our customers to the best.Money is the source nowdays, the trust of how we can use your money in the best way is build by us.The core value of php soft solutions is to provide the best service to our customers in a great manner.

Contact

PLOT NO. C157, The Groove, Industrial focal point, Phase7,MOHALI, SAS Nagar, Punjab, 160055

8146501814

contact@phpsoftsolutions.in

Pages Links
  • WordPress
  • Yii
  • PHP Frameworks
  • php-jquery
  • system
  • phonegap

© 2025 PHPSOFT SOLUTIONS