LoginSignup
1
0

More than 3 years have passed since last update.

GoogleのCustom Search APIでニュースを検索する方法

Posted at

方法 : 検索エンジンの設定でschema.orgタイプにニュース関連のノードを設定する

  1. Programmable Search - 検索エンジンの編集から作った検索エンジンを選択
  2. [schema.org タイプを使用しているページを制限する]で「schema.orgタイプ」を選択する
  3. 保存は自動でされる

a.gif

やりたいこと : WordPressのページに検索したニュースを表示したい

WordPressもCustom Search APIも超初心者の状態で進めていた。

  1. WordPressにショートコードを使ってPHPを埋め込む方法 - Qiitaを覚えた
  2. WordPressでGoogleのCustom Search APIを使う方法 - ponsuke_tarou’s blogを覚えた
  3. GoogleのCustom Search APIでORとAND検索する方法 - Qiitaを覚えた
  4. で、普通に検索した時に[ニュース]のところに表示されるみたいなのをWordPressのページに表示してみたかった

image.png

API Reference  |  Custom Search  |  Google Developersを読んでもよくわからなかった・・・(英語よくわかんないし)

そんな時に「schema.orgタイプ」でできそうと知った。

You can use schemas from Schema.org to specify if you're only looking for articles/ blog posts.
To specify the Schema, go to Advanced settings on the Control Panel and select the required schema from here.
Google custom search engine for Google News - Stack Overflow

「schema.orgタイプ」が何だかはよくわからないけど使ってみる

schema.org について
schema.org とは、Google など大手の検索プロバイダにより認められた方法で、コンテンツをマークアップするためにウェブマスターに HTML を提供する取り組みです。たとえば、映画に関するサイトのウェブマスターはサイトの HTML で「Movie」というスキーマタイプと、「director」や「genre」などのプロパティを使用できます。検索プロバイダはそのデータを使用して、検索結果に表示されるページのコンテンツを把握することができます
schema.org タイプを使ってトピックの検索エンジンを作成 - Programmable Search Engine ヘルプ

やってみた

  • 環境
    • Windows 10 Pro 64bit バージョン1909
    • Local by Flywheel 5.6.1
      • PHP 7.4.1 / MySQL 8.0.16 / Apach / WordPress 5.4.2
      • テーマ : Lightning

PHPのコードは同じで検索エンジンのschema.org タイプを変えて表示してみた

schema.org タイプを指定しない場合

Qiitaとかブログの検索結果が出てくる
image.png

schema.org タイプを指定した場合

ニュースだけとはいかないけどニュースっぽい検索結果が出た!
image.png

使ったコード

コードでは特段「ニュース」とか関係なく、ただ検索しているだけ。

google-search.php
<?php
// 検索ワード群1
$word_group1 = array('ponsuke','tarou','0531','ぽんすけ','たろう');
// 検索ワード群2
$word_group2 = array('Qiita','hatena');
// 検索ワード群1~2はOR条件で検索したい
$word_or1 = implode(' OR ', $word_group1);
$word_or2 = implode(' OR ', $word_group2);
// 検索ワード群1と2のどちらの検索文言も含んで検索したい : (検索ワード群1) AND (検索ワード群2)
$search_words = '(' . $word_or1 . ') AND (' . $word_or2 . ')';
$param_ary = array(
    // 検索ワード
    'q' => $search_words,
    'key' => {APIキー},
    'cx' => {検索エンジンID},
    // JSON形式で取得する
    'alt' => 'json',
    // 取得開始順位(検索結果1~10位を取得するを取得する)
    'start' => 1
);
$param = http_build_query($param_ary);
$reqest_url = 'https://www.googleapis.com/customsearch/v1?' . $param;
$result_json = file_get_contents($reqest_url, true);
$result = json_decode($result_json, true);
$items = $result['items'];
?>

<h1>今日のぽんすけ検索</h1>
<?php if (count($items) === 0) : ?>
<p>ぽんすけ情報はありません。</p>
<?php else : ?>

<?php foreach ($items as $key => $item) : ?>
<a href="<?php echo $item['link']; ?>"><?php echo $item['title']; ?></a>
<br>
<?php endforeach; ?>

<?php endif; ?>
functions.php
<?php
// ...省略...
function google_search() {
    // 出力バッファリングを有効化する
    ob_start();
    // 外部ファイルを読み込む
    get_template_part('google-search');
    // 出力バッファを削除する
    return ob_get_clean();
}
add_shortcode('google-search', 'google_search');
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0