Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 如何防止wp_insert_post()设置未分类的类别?_Wordpress_Post_Insert - Fatal编程技术网

Wordpress 如何防止wp_insert_post()设置未分类的类别?

Wordpress 如何防止wp_insert_post()设置未分类的类别?,wordpress,post,insert,Wordpress,Post,Insert,我使用的是Wordpress 3.5,似乎wp\u insert\u post()无法再设置类别,文档系统如下: post_类别不再存在,请尝试wp_set_post_terms()以设置 邮政类别 问题是wp\u set\u post\u terms()或wp\u set\u object\u terms()需要posted,它由wp\u insert\u post()返回。虽然可以将类别术语设置为通过wp\u insert\u post()插入的帖子,但问题是每次调用wp\u insert\

我使用的是
Wordpress 3.5
,似乎
wp\u insert\u post()
无法再设置类别,文档系统如下:

post_类别不再存在,请尝试wp_set_post_terms()以设置 邮政类别


问题是
wp\u set\u post\u terms()
wp\u set\u object\u terms()
需要
posted
,它由
wp\u insert\u post()返回。虽然可以将类别术语设置为通过
wp\u insert\u post()
插入的帖子,但问题是每次调用
wp\u insert\u post()
时,除了调用
wp\u insert\u post()
后设置的类别术语之外,我的帖子中还包含
未分类的
类别。如何防止未分类的
始终存在?

我不知道您在哪里发现
wp\u insert\u post()不能再设置类别了
,但是从

// Create post object
$my_post = array(
    'post_title'    => 'My post',
    'post_content'  => 'This is my post.',
    'post_status'   => 'publish',
    'post_author'   => 1,
    'post_category' => array(8,39) // id's of categories
);

// Insert the post into the database
wp_insert_post( $my_post );
Bellow是我的一个工作示例,我在我的一个站点中使用它,由一个管理员动态添加新帖子,其类别名称为
location
,带有两个元字段,输入来自用户(我已经过滤了用户输入,但这里省略了)

还请记住,每次添加没有类别的新帖子时,
WordPress
都会设置该帖子的默认类别,并且该帖子是
uncategorized
如果您没有从“管理”面板中进行更改,则可以将默认类别从
uncategorized
更改为您想要的任何内容

更新: 由于
post\u类别已不存在,因此您可以替换

'post_category' => array($new_cat_ID)
有以下几点

'tax_input' => array( 'category' => $new_cat_ID )
在上面给出的例子中。你也可以使用

$newpost_id=wp_insert_post($my_post);
wp_set_post_terms( $newpost_id, array($new_cat_ID), 'category' );
请记住,在本例中,已使用以下代码行找到
$new\u cat\u ID

$new_cat_ID = get_cat_ID($category);
但也可以使用以下代码获取类别id

$category_name='location';
$term=get_term_by('name', $category_name, 'category');
$cat_ID = $term->term_id;

阅读有关函数的更多信息。

我不知道您在哪里发现
wp\u insert\u post()不能再设置类别了
,但从

// Create post object
$my_post = array(
    'post_title'    => 'My post',
    'post_content'  => 'This is my post.',
    'post_status'   => 'publish',
    'post_author'   => 1,
    'post_category' => array(8,39) // id's of categories
);

// Insert the post into the database
wp_insert_post( $my_post );
Bellow是我的一个工作示例,我在我的一个站点中使用它,由一个管理员动态添加新帖子,其类别名称为
location
,带有两个元字段,输入来自用户(我已经过滤了用户输入,但这里省略了)

还请记住,每次添加没有类别的新帖子时,
WordPress
都会设置该帖子的默认类别,并且该帖子是
uncategorized
如果您没有从“管理”面板中进行更改,则可以将默认类别从
uncategorized
更改为您想要的任何内容

更新: 由于
post\u类别已不存在,因此您可以替换

'post_category' => array($new_cat_ID)
有以下几点

'tax_input' => array( 'category' => $new_cat_ID )
在上面给出的例子中。你也可以使用

$newpost_id=wp_insert_post($my_post);
wp_set_post_terms( $newpost_id, array($new_cat_ID), 'category' );
请记住,在本例中,已使用以下代码行找到
$new\u cat\u ID

$new_cat_ID = get_cat_ID($category);
但也可以使用以下代码获取类别id

$category_name='location';
$term=get_term_by('name', $category_name, 'category');
$cat_ID = $term->term_id;

了解更多有关函数的信息。

谢谢!在您所指的Wordpress文档页面上,您可以阅读“//post\u category不再存在,请尝试在关键字的注释中使用wp\u set\u post\u terms()设置帖子的类别。谢谢!在您指向的Wordpress文档页面上,您可以阅读“//post\u类别不再存在,请尝试在键的注释中使用wp\u set\u post\u terms()设置帖子的类别”。