你好
我找到了一个可以工作的代码,但它创建了几个同名的帖子,我不想这样,如果值为“x”,他只想创建一次post\\u类型,但每次更新时他都会创建一个“x”,我如何只创建一次此post\\u类型
My Code:
<?php
wp_insert_post( array(
\'post_status\' => \'publish\',
\'post_type\' => \'fornecedores\', // post_type which is created posting
\'post_title\' => get_field(\'fornecedor\'), // Custom field to generate the post type
\'post_content\' => \'\'
)
)
?>
代码位于post类型产品的循环中
明白我的意思吗?
最合适的回答,由SO网友:gmazzap 整理而成
插入前检查是否存在post:
$exists = get_page_by_title( get_field(\'fornecedor\'), OBJECT, \'fornecedores\');
if ( empty( $exists ) ) {
wp_insert_post( array(
\'post_status\' => \'publish\',
\'post_type\' => \'fornecedores\', // post_type which is created posting
\'post_title\' => get_field(\'fornecedor\'), // Custom field to generate the post type
\'post_content\' => \'\'
)
}