最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - Adding Plugin-specific Fields with wp_insert_post()?

matteradmin8PV0评论

(Moderator's note: The original title was "wp_insert_post() and plugins")

I use wp_insert_post() like this:

 $my_post = array(
 'post_title' => 'title',
 'post_content' => $post,
 'post_status' => 'publish',
 'post_author' => 1,
 'post_date' => date('Y-m-d H:i:s', $oldtime),
 'post_category' => array(3,4)

 );

 wp_insert_post( $my_post );

Everything works fine, but I want to use plugins like "All in One SEO Pack" I can't figure out how to save their custom fields. I tried this but it did not work:

 $my_post = array(
 'post_title' => 'title',
 'post_content' => $post,
 'post_status' => 'publish',
 'post_author' => 1,
 'post_date' => date('Y-m-d H:i:s', $oldtime),
 'post_category' => array(3,4)
 'aiosp_description' => 'description',
 'aiosp_keywords' => 'keywords' // these (aiosp_) is from post edit page in WP (<input type="text" size="62" name="aiosp_keywords" value="keywords">)

 );

 wp_insert_post( $my_post );

How can I save these fields? Thanks for any help!!!

(Moderator's note: The original title was "wp_insert_post() and plugins")

I use wp_insert_post() like this:

 $my_post = array(
 'post_title' => 'title',
 'post_content' => $post,
 'post_status' => 'publish',
 'post_author' => 1,
 'post_date' => date('Y-m-d H:i:s', $oldtime),
 'post_category' => array(3,4)

 );

 wp_insert_post( $my_post );

Everything works fine, but I want to use plugins like "All in One SEO Pack" I can't figure out how to save their custom fields. I tried this but it did not work:

 $my_post = array(
 'post_title' => 'title',
 'post_content' => $post,
 'post_status' => 'publish',
 'post_author' => 1,
 'post_date' => date('Y-m-d H:i:s', $oldtime),
 'post_category' => array(3,4)
 'aiosp_description' => 'description',
 'aiosp_keywords' => 'keywords' // these (aiosp_) is from post edit page in WP (<input type="text" size="62" name="aiosp_keywords" value="keywords">)

 );

 wp_insert_post( $my_post );

How can I save these fields? Thanks for any help!!!

Share Improve this question edited Mar 27, 2013 at 3:16 Grant Palin 1,0492 gold badges13 silver badges35 bronze badges asked Oct 30, 2010 at 21:07 wordword
Add a comment  | 

2 Answers 2

Reset to default 3

@sorich87's answer is 99% there. The difference is that All-in-One SEO Pack follows certain best practices and uses a prefix of '_aioseop_' on its meta keys. That makes the actual working code look more like this:

$my_post = array(
  'post_title' => 'title',
  'post_content' => $post,
  'post_status' => 'publish',
  'post_author' => 1,
  'post_date' => date('Y-m-d H:i:s', $oldtime),
  'post_category' => array(3,4)
);

$post_id = wp_insert_post( $my_post );

if( !is_wp_error($post_id) && $post_id > 0 ) {
  add_post_meta($post_id, '_aioseop_keywords', $keywords);
  add_post_meta($post_id, '_aioseop_description', $description);
  add_post_meta($post_id, '_aioseop_title', $title);
}

Here's a screenshot of the All-in-One SEO Pack specific records in the wp_postmeta table of my test system using Navicat for MySQL to view them:


(source: mikeschinkel)

All-in-one SEO data are saved as post metas. You will need to use add_post_meta:

$my_post = array(
    'post_title' => 'title',
    'post_content' => $post,
    'post_status' => 'publish',
    'post_author' => 1,
    'post_date' => date('Y-m-d H:i:s', $oldtime),
    'post_category' => array(3,4)
);

$post_id = wp_insert_post( $my_post );

if( !is_wp_error($post_id) && $post_id > 0 ) {
    add_post_meta($post_id, 'keywords', $keywords);
    add_post_meta($post_id, 'description', $description);
    add_post_meta($post_id, 'title', $title);
}
Post a comment

comment list (0)

  1. No comments so far