我编写了在添加帖子时检查自定义帖子标题是否存在的功能。如下所示:
function wp_exist_post_by_title( $title ) {
global $wpdb;
$return = $wpdb->get_row( "SELECT ID FROM wp_dxwe_posts WHERE post_title = \'" . $title . "\' && post_status = \'publish\' && post_type = \'courses\' ", \'ARRAY_N\' );
if( empty( $return ) ) {
return false;
} else {
return true;
}
}
添加新帖子时,我的代码如下:
$check = $this->wp_exist_post_by_title( $_POST[\'course_title\'] ) ;
if($check==true)
{
$title = \'\';
echo "<div class=\'conditional-messages\'>Course Already Exists !</div>";
}
else
{
$title=$_POST[\'course_title\'];
}
$post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_status\' => \'publish\',
\'post_type\' => "courses"
);
$description = $_POST[\'description\'];
$id = wp_insert_post($post);
update_post_meta($id, \'course_icon\', $attachement_id);
update_post_meta( $id, \'inistitute_name\', $institute_name);
当我添加新帖子(课程标题)时,它正在检查帖子标题是否存在或不完美,但我这里的问题是,我想检查帖子标题(课程标题)只属于一个“institute\\u name”,因为其他学院也可以有相同的课程标题(自定义帖子)。我知道我需要连接两个表,即“wp\\u posts”&;\'wp\\u Posteta’,但我无法做到。谁能告诉我怎么解决这个问题吗?谢谢你的帮助。
SO网友:Amer Ahmed
使用post\\u exists()函数,您可以找出您的任何帖子是否有特定标题。
Code Reference post_exists()
来自“评论”;“Amirhosseinhpv”;
按标题检查是否存在帖子:
$found_post = post_exists( "My Post Title",\'\',\'\',\'\');
echo $found_post ? "Found post at id #$found_post" : "Can\'t find post!";
按标题检查是否存在自定义帖子类型[新闻]:
$found_post = post_exists( "My Post Title",\'\',\'\',\'news\');
echo $found_post ? "Found post at id #$found_post" : "Can\'t find post!";
您需要包括“wp admin/includes/post”。php“如果您在前端执行以下检查:
require\\u once(ABSPATH.“wp admin/includes/post.php”);