wp_update_post() 错误

wp_update_post() Error

我有一个自定义代码,可以自动创建菜单并更新 'post' table 以发布我的菜单。这是我的代码。

$test = array( 
array(
'menu-item-db-id' => 0,
'menu-item-object-id' => 2,
'menu-item-object' => 'category',
'menu-item-parent-id' => 0,
'menu-item-type' => 'taxonomy',
'menu-item-title' => 'Rome',
'menu-item-url' => 'http://test.exmple.com/rome/',
'menu-item-target' => '',
'menu-item-classes' => '', 
'menu-item-xfn' => '',
'menu-item-description' => ''
)
);


if($item_ids = wp_save_nav_menu_items( 7, $test )) {
   $my_post = array (
    'ID' => $item_ids,
    'post_status' => 'publish',
    'post_name' => $item_ids
);
  wp_update_post($my_post);
}

这里的问题是在调用函数wp_update_post时出现错误:

Warning: strip_tags() expects parameter 1 to be string, array given in      /home/lingfish/public_html/wp-includes/formatting.php on line 1068

Warning: urlencode() expects parameter 1 to be string, array given in /home/lingfish/public_html/wp-includes/post.php on line 2790

Warning: preg_match() expects parameter 2 to be string, array given in /home/lingfish/public_html/wp-includes/formatting.php on line 633

Warning: strip_tags() expects parameter 1 to be string, array given in /home/lingfish/public_html/wp-includes/formatting.php on line 1068

然而,在加载页面之后,当我刷新但评论除此之外的所有代码时:

$my_post = array (
     'ID' => $item_ids,
     'post_status' => 'publish',
     'post_name' => $item_ids
);
 wp_update_post($my_post);

它会工作,我有填充,在保存菜单项后它不会在 post table 上自动创建这就是为什么调用 wp_update_post 没有结果.您是否有其他解决方案以便我可以更新字段 post_status 以发布以便我的菜单可见?

您不能将数组放入 wp_update_post。您必须遍历 $item_ids 并改用字符串值。例子;

if($item_ids = wp_save_nav_menu_items( 7, $test )) {

  foreach( $item_ids AS $single_item_id ) {

    $my_post = array (
     'ID' => $single_item_id,
     'post_status' => 'publish',
     'post_name' => $single_item_id
    );

    wp_update_post($my_post);

  }

}
$my_post = array (
     'ID' => $item_ids,
     'post_status' => 'publish',
          'post_name' => $item_ids
);
wp_update_post($my_post);

问题是行:post_name' => $item_ids wp 想要一个字符串,而你给了它一个数组。 遍历 ID 并一一更改。