<?php
class WPIsertPostTest extends WP_UnitTestCase {
/**
* @topic 159088
*/
function test_159088() {
$my_post = array(
'post_title' => 'あいう',
'post_content' => "かきく + " . date( 'r' ),
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 1 )
);
$my_post_id = wp_insert_post( $my_post );
$this->assertEquals( mb_strtolower(urlencode('あいう')), get_post( $my_post_id )->post_name );
}
}
OS X, PHP 7 でテストしました。
このテスト書いて通ったんで、コードがバグってるとかでは無いと思われます。
post_name が正しく、「あいう」をURLエンコードして小文字にした奴と一致するかどうかをテストしてます。
2回繰り返すと、ID違いの投稿が正しく2つできるのですが、スラッグだけは post-0 になり重複するんです。
こんにちは
for ($i = 0;$i < 10;$i++){
$my_post = array(
'post_title' => 'あいう',
'post_content' => "かきく + " . date( 'r' ),
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 1 )
);
// Insert the post into the database
var_dump( wp_insert_post( $my_post ) );
}
を試しましたが、結果は、
int(29267) int(29268) int(29269) int(29270) int(29271) int(29272) int(29273) int(29274) int(29275) int(29276)
で、取れているみたいでした
一回だけ、
$my_post = array(
'post_title' => 'あいう',
'post_content' => "かきく + " . date( 'r' ),
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 1 )
);
// Insert the post into the database
var_dump( wp_insert_post( $my_post ) );
結果
int(29307)
このコードで確認すると再現します。
for ( $i = 0; $i < 3; $i++ ) {
$my_post = array(
'post_title' => 'あいう',
'post_content' => "かきく + " . date( 'r' ),
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 1 )
);
$my_post2 = $my_post;
$my_post2['post_title'] = 'ABCDEFG';
$my_id = wp_insert_post( $my_post );
$my_id2 = wp_insert_post( $my_post2 );
$my_slug = get_post( $my_id )->post_name;
$my_slug2 = get_post( $my_id2 )->post_name;
echo "<div>id: $my_id = slug: $my_slug</div>";
echo "<div>id2: $my_id2 = slug2: $my_slug2</div>";
}
WordPress 4.5:
id: 558 = slug: post-0
id2: 559 = slug2: abcdefg
id: 560 = slug: post-0
id2: 561 = slug2: abcdefg-2
id: 562 = slug: post-0
id2: 563 = slug2: abcdefg-3
WordPress 4.4.2:
id: 1367 = slug: %e3%81%82%e3%81%84%e3%81%86
id2: 1368 = slug2: abcdefg
id: 1369 = slug: %e3%81%82%e3%81%84%e3%81%86-2
id2: 1370 = slug2: abcdefg-2
id: 1371 = slug: %e3%81%82%e3%81%84%e3%81%86-3
id2: 1372 = slug2: abcdefg-3
お騒がせして申し訳ありませんでした。
テーマを Twenty Sixteen に変えてプラグインをすべて停止(この検証用のものだけ有効)すると、正常になりました。テーマが Simplicity2 の時に発生していました。
追記:Simplicity2 の設定項目「日本語のスラッグを有効にする」のチェックが外れているときだけ発生します。同テーマのバージョン 2.1.0 20160227p で確認しました。WordPress 4.4.2 / 4.5 どちらでも同じです。後ほど作者さんのサイトへ報告します。