Tôi có một trang wordpress kết nối với một máy chủ xà phòng. Vấn đề là mỗi lần tôi chạy kịch bản "wp_insert_post" đang sử dụng cùng một kết quả một lần nữa. Tôi muốn kiểm tra xem post_title hiện có có khớp với giá trị từ $ title sau đó nếu chúng khớp nhau, hãy ngăn wp_insert_post sử dụng lại cùng một giá trị.Làm thế nào để ngăn chặn bài đăng trùng lặp bằng cách kiểm tra xem tiêu đề bài có tồn tại trước khi chạy "wp_insert_post" không?
Dưới đây là các mã:
try {
$client = new SoapClient($wsdl, array('login' => $username, 'password' => $password));
} catch(Exception $e) {
die('Couldn\'t establish connection to weblink service.');
}
$publications = $client->GetPublicationSummaries();
foreach ($publications->GetPublicationSummariesResult->PublicationSummaries->PublicationSummary as $publication_summary) {
// get the complete publication from the webservice
$publication = $client->getPublication(array('PublicationId' => $publication_summary->ID))->GetPublicationResult->Publication;
// get all properties and put them in an array
$properties = array();
foreach ($publication->Property as $attribute => $value) {
$properties[$attribute] = $value;
}
// Assemble basic title from properties
$title = $properties['Address']->Street . ' ' . $properties['Address']->HouseNumber . $properties['Address']->HouseNumberExtension . ', ' . $properties['Address']->City->_;
}
$my_post = array(
'post_title'=>$title,
'post_content'=>'my contents',
'post_status'=>'draft',
'post_type'=>'skarabeepublication',
'post_author'=>1,
);
wp_insert_post($my_post);
Cảm ơn bạn đã giúp đỡ nào.
bạn nên thử mã này. yêu cầu (dirname (__ FILE__). '/wp-load.php'); toàn cầu $ wpdb; echo $ count = $ wpdb-> get_var ("chọn COUNT (*) từ $ wpdb-> bài đăng trong đó' post_title' like '$ title' "); – Robot