以前のここの記事で、function.phpに直接追加するカスタム投稿の方法を示したのでR。
しかし、
カスタム投稿の場合は、検索で引っかからないこともあるのでR。
'exclude_from_search' => false,
をカスタム投稿に追加して、
add_action( 'init', 'custom_post_type' );
function custom_post_type() {
register_post_type( 'custompost',
array('labels' =>
array(
'name' => __( 'カスタム投稿' ),
'singular_name' => __( 'カスタム投稿' )
),
'public' => true,
'menu_position' => 5,
'hierarchicla' => false,
'has_archive' => true,
'exclude_from_search' => false,
'supports' => array('title','editor','thumbnail',
'custom-fields','excerpt','author','trackbacks',
'comments','revisions','page-attributes')
)
);
}
としたのでR。
しかし、
検索でカスタム投稿が引っかからないのでR。
そこで、ここを参考に、function.phpに
function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'custompost'));
};
return $query;
};
add_filter('pre_get_posts', 'filter_search');
を追加すると、できたのでR。
上の追加は、
'exclude_from_search' => false,
がなくても問題なかったのでR。
実は、プラグインで実現したカスタム投稿はここで記事にしていたのでRが、このプラグインに対する検索の解決もここで記事にしていたのでR。
うっかりしていたが、実は、ソースをいじるカスタム投稿でもこの方法は有効だったのでR。そのため、ここでは、ver.2として、記事にしたのでR。

この記事をシェアできます。