我使用以下代码发布到wordpress
<?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;
}
?>
如何避免发布重复的标题。例如,如果我已经有标题为Test it
的帖子。当我尝试发布标题为Test it
的其他帖子时,不应发帖。
P.s:我的博客中有1000条帖子。
答案 0 :(得分: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
}
答案 1 :(得分: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;
}