Skip to content

関連投稿でpost-typeが指定されていないとFatalになる #110

@yousan

Description

@yousan

概要

4.2.2 から 5.0.0 にアップデートした際、関連投稿 がFatalErrorで落ちてしまいました。
確認したところ 関連投稿 の対象となる post-type を指定していないとFatalになってしまうようでした。

詳細

下記のようなコードの場合にFatalになります。

$Setting->add_group(
	'relation-without-post-type-string',
	false,
	array(
		array(
			'name'    => 'text-has-default',
			'label'   => 'text has default',
			'type'    => 'relation',
			'default' => 'a',
			// 'post-type' => 'post'    // この指定がなかった
		),
	)
);

4.2.2では動いていたのですが、5.0.0で動かなくなった原因は、無指定だった場合に post の指定になるようになっていた箇所がなくなっていたためのようでした。

その上でエラーは implode に文字列が渡されていてFatalが起きていました。

https://github.com/inc2734/smart-custom-fields/blob/2de9335232b25056bce9ecedb564867d9850d8e8/classes/fields/class.field-related-posts.php#L268C3-L268C31

エラーメッセージ

[20-Jun-2025 05:30:57 UTC] PHP Fatal error:  Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in ... /wp-content/plugins/smart-custom-fields/classes/fields/class.field-related-posts.php:271
Stack trace:
#0 ...plugins/smart-custom-fields/classes/fields/class.field-related-posts.php(271): implode(',', '')
#1 .../wp-content/plugins/smart-custom-fields/classes/controller/class.controller-base.php(317): Smart_Custom_Fields_Field_Related_Posts->get_field(0, Array)
#2 .../wp-content/plugins/smart-custom-fields/classes/controller/class.controller-base.php(90): Smart_Custom_Fields_Controller_Base->display_tr(Object(WP_Post), false, Array, 0)
#3 .../wp/wp-admin/includes/template.php(1456): Smart_Custom_Fields_Controller_Base->display_meta_box(Object(WP_Post), Array)
#4 .../wp/wp-admin/includes/post.php(2394): do_meta_boxes(Object(WP_Screen), 'normal', Object(WP_Post))
#5 .../wp/wp-admin/edit-form-blocks.php(383): the_block_editor_meta_boxes()
#6 .../wp/wp-admin/post-new.php(72): require('/var/web/wp/wp-...')
#7 {main}
  thrown in .../wp/wp-content/plugins/smart-custom-fields/classes/fields/class.field-related-posts.php on line 271

該当コミット

回避方法

回避方法を確認しました。

  1. add_group時に投稿タイプを指定する
  2. post-type がない場合に post の指定になるようにする(4.2.2の状態に戻す)
  3. optionspost-typearray()にする
  4. implode の第二引数arrayにキャストする

質問

以上のような経緯でして add_group で投稿タイプを指定すれば問題は回避されそうなのですが、本体側での対応(上記の2~3のいずれか)はされますか?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions