0

以下のコードを使用してワードプレスに投稿しています

   <?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れません。

追伸:ブログに1000件の投稿があります。

4

2 に答える 2

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

}
于 2012-12-03T20:28:21.737 に答える
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;
}
于 2015-03-25T05:29:15.220 に答える