我正在尝试创建php代码,但它对我不起作用。它必须在我的网站数据库中检查"post_title",然后检查"if exist{do}(while)"if not exist create post。请看我的代码
?php
include 'wp-config.php';
$id = "id";
$nu = "2";
$selectquery ="SELECT * FROM `wpop_posts` WHERE `post_title` = '$id$nu' ";
$selectresult=mysql_query($selectquery);
while(mysql_num_rows($selectresult)>0)
{
$errormsgnewemployee = '<p id=notification>it exist.</p>';
echo "$errormsgnewemployee";
++$nu;
echo" $nu";
};
if (!mysql_num_rows($selectresult)>0) {$my_post = array();
$my_post['post_title'] = $id .$nu;
$my_post['post_content'] = $shelma;
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_category'] = array(8,39);
wp_insert_post( $my_post );
print 'thats good!';};
?>
没有理由为此使用原始SQL。实际上,Wordpress中已经有一个函数可以做到这一点:get_page_by_title()
。只需将标题作为字符串参数传递到函数中,它就会返回匹配帖子的帖子对象。如果多个帖子共享相同的标题,它只会返回一个帖子数组。