WordPressの関連記事をカテゴリーとタグからプラグインなしで取得する(自作)

global $wpdb;
$sql = 'SELECT object_id,COUNT(object_id) AS c FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id = '.get_the_ID().') GROUP BY object_id ORDER BY c DESC, object_id DESC LIMIT 12;';
$object = $wpdb->get_results($sql);
$ids = [];
foreach($object as $v){
  if(get_the_ID() == $v->object_id){
    continue;
  }
  $ids[] = $v->object_id;
}
$params = [
'post_type' => 'post',
'include'=> $ids,
'orderby' => 'post__in',
];
$relposts = get_posts($params);
foreach ( array_slice($relposts,0,6) as $post ) :
  setup_postdata( $post );
endforeach;


SELECT object_id,COUNT(object_id) AS c FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id = '.get_the_ID().') GROUP BY object_id ORDER BY c DESC, object_id DESC LIMIT 12;

このSQLが肝
このSQLでterm一覧を取得しその中の記事を集計して関連度の高い順に取得している
includeを使うとexcludeが効かないので取得時に除外
記事数も制御できないのでarray_sliceでカット
多めに取得しているのは非公開記事や固定ページを弾くため

UbuntuにLaravelのインストール

公式

composerのインストール

curl -sS https://getcomposer.org/installer | php
sudo   mv composer.phar /usr/local/bin/composer

laravelのインストール

composer global require "laravel/installer=~1.1"

path通す
~/.composer/vendor/binではないので注意

export PATH="$PATH:~/.config/composer/vendor/bin"

プロジェクト作成

laravel new hoge

keyの生成
The only supported ciphers are AES-128-CBC and AES-256-CBC

php artisan key:generate
php artisan config:clear

nginx

server {
    listen       80;
    server_name  hogehoge.com;


    root   /var/www/hoge/public;
    index  index.php;

    location / {
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
      fastcgi_pass   unix:/run/php/php7.2-fpm.sock;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
    }
}

file_get_contents()をcurl化

function getCurl($url,array $opt = []){
    $defaults = [
        CURLOPT_URL => $url,
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_TIMEOUT => 5
    ];
    $ch = curl_init();
    curl_setopt_array($ch, ($opt + $defaults));
    if(!$result = curl_exec($ch)){
        error_log('Curl error: '.curl_error($ch).' url: '.$url);
    }
    curl_close($ch);
    return $result;
}
echo getCurl('https://wp.wkbr.net');

wordpressでURLに新規パラメータを追加 スマートフォンでページを分ける

例えばスマホページ等でページを分ける
パラメーターsmpがあったらスマホ用のsmp-single.phpを読み込む

// URLへのパラメーター追加を許可
add_action('init',function(){
    add_rewrite_endpoint('smp', EP_PERMALINK);
    //flush_rewrite_rules(); // DBの更新だが初回のみでよいのでコメントアウト
});
// スマホ用テンプレートの選択
add_action('template_redirect',function(){
    if(!is_single()){return;}
    $smp = get_query_var('smp', false);
    if(!$smp){return;}
    include(get_template_directory().'/smp-single.php');
    exit;
});
// パラメーターの追加
add_filter('query_vars',function($vars){
    $vars[] = 'smp';
    return $vars;
});
// urlの場合はパラメーターが空になるのでtrueに上書き
add_filter( 'request',function($vars){
    if ( isset( $vars['smp'] ) && '' === $vars['smp'] ) { 
        $vars['smp'] = 1;
    }   
    return $vars;
});