2011-10-04 10 views
5

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.

+2

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

Trả lời

4

Xin lỗi vì sự chậm trả lời. Tôi đã sử dụng những gì mà Robot nói trong phần bình luận và điều này giải quyết được vấn đề của tôi. Cảm ơn

$post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_title like '$title_from_soap'"); 
if($post_if < 1){ 
    //code here 
} 
13

Bạn có thể sử dụng get_page_by_title() vì nó hỗ trợ các loại bài đăng tùy chỉnh ngay bây giờ.

if (!get_page_by_title($title, OBJECT, 'skarabeepublication')) : 

    $my_post = array(
     'post_title'=>$title, 
     'post_content'=>'my contents', 
     'post_status'=>'draft', 
     'post_type'=>'skarabeepublication', 
     'post_author'=>1, 
    ); 
    wp_insert_post($my_post); 

endif; 

thông tin Codex here

+2

Tôi thích phương pháp này để tận dụng các chức năng tích hợp để có kết quả tốt nhất. Tôi tin rằng dòng đầu tiên nên là: "if (! Get_page_by_title ($ title, OBJECT, 'skarabeepublication')):" – Jake

+0

Bạn hoàn toàn chính xác, cảm ơn! – CookiesForDevo

+0

'OBJECT' không nên có dấu nháy đơn, nhưng nếu không phương pháp này hoạt động hoàn hảo và vẫn còn hợp lệ như của WordPress 4.7.3. – Arinthros

4

Ngạc nhiên khi không đề cập đến chức năng post_exists trong wp-includes/post.php. Xem entry on wpseek. Không có mục nào trong codex. Tại nó đơn giản nhất nó hoạt động như get_page_by_title nhưng trả về một id bài viết (hoặc 0 nếu không tìm thấy) thay vì đối tượng (hoặc null).

$post_id = post_exists($my_title); 
if (!$post_id) { 
    // code here 
} 
+0

Hiện tại có [mục nhập trên post_exists] (https://developer.wordpress.org/reference/functions/post_exists/) trong Tham chiếu mã nhà phát triển Wordpress. – Jon

0

sampler:

if(!get_page_by_path('mypageslug',OBJECT,'post')){ 
    //your codes 
}