2012-12-03 41 views
0

我使用下面的代碼檢測重複的標題 - WordPress的RPC

<?php 
      require("class-IXR.php"); 
      $client = new IXR_Client('http://domain.com/xmlrpc.php'); 

      $USER = 'user'; 
      $PASS = 'pass'; 

      $content['title'] = 'Test title'; 
      $content['categories'] = array("games"); 
      $content['description'] = '<p>Lorem ipsum dolor sit amet</p>'; 
      $content['custom_fields'] = array(array('key' => 'my_custom_fied','value'=>'yes')); 
      $content['mt_keywords'] = array('foo','bar'); 

      if (!$client->query('metaWeblog.newPost','', $USER,$PASS, $content, true)) 
      { 
       die('Error while creating a new post' . $client->getErrorCode() ." : ". $client->getErrorMessage()); 
      } 
      $ID = $client->getResponse(); 

      if($ID) 
      { 
       echo 'Post published with ID:#'.$ID; 
      } 

    ?> 

如何避免重複發帖例如title.For如果我已經有帖子標題爲Test it發佈使用WordPress。當我嘗試張貼另一篇文章標題Test it它不應該發佈。

P.s:我的博客中有1000篇文章。

回答

0
if (is_null(get_page_by_title($content['title'], OBJECT, 'post'))) { 

// Do your stuff in this case (no post with the same title) 

} 

else { 

// Do not create post 

} 
+0

輝煌:)愛你 –

+0

這將工作,如果你在WordPress內,但不在API。 – timgws

0

如果要檢查使用WordPress的XML-RPC API重複WordPress的帖子,你將需要使用wp.getPosts獲得已作出所有的WordPress的職位列表,然後循環通過所有的職位,以確保沒有重複。

如在https://codex.wordpress.org/XML-RPC_WordPress_API/Posts中看到的,您需要先檢索所有博客文章的列表,然後遍歷所有博文,以確定post_title或post_name是否已設置。

$result = $client->query('wp.getPosts','', $USER, $PASS) 
if (!$result) { 
    die('Error while getting posts' . $client->getErrorMessage()); 
} 

$posts = $client->message->params[0]; 

$title_to_search_for = 'This is a duplicate title '; 

// loop through all of the $posts, see if the title is duped. 
$title_to_search_for = trim(strtolower($title_to_search_for)); 

$is_duplicate = false; 

foreach($posts as $post) { 
    if (trim(strtolower($post->post_title) === $title_to_search_for) 
     $is_duplicate = true; 

    if (trim(strtolower($post->post_name) === $title_to_search_for) 
     $is_duplicate = true; 
}