- Custom DataBase Tables
- CDBT Version 2.x
- フィルターフック一覧
- cdbt_shortcode_query_conditions
cdbt_shortcode_query_conditions
(CDBT 2.1.32)
cdbt_shortcode_query_conditionsショートコードのデータ取得用SQLクエリの検索条件をフィルターする
Description
array cdbt_shortcode_query_conditions ( array $conditions [, string $narrow_operator, string $shortcode_name, string $table ] )ショートコードでデータ取得用のSQLクエリの検索条件(WHERE句)をフィルターする。このフィルターフックを使用することで、
[cdbt-view]もしくは[cdbt-edit]で指定された検索条件をカスタマイズできます。Parameters
- conditions
- ショートコードの属性「narrow_keyword」として指定された検索条件の配列。検索条件が指定されなかった場合はNULLになります。
- narrow_operator
- 複数の絞り込み条件がある場合に有効な条件結合子の種別。andもしくはorのどちらかの文字になります。
- shortcode_name
- フィルター後にレンダリングされるショートコード名を参照できます。
- table
- ショートコードが処理するテーブル名を参照できます。
History
| バージョン | 内容 |
|---|---|
| 2.1.32 | 新規追加 |
Example
1. [[cdbt-view table=”prefix_table”]]および[[cdbt-edit table=”prefix_table”]]にて検索条件に現在ログインしているユーザーIDを設定する。
<?php
function my_filter_cdbt_shortcode_query_conditions( $conditions, $narrow_operator, $shortcode_name, $table ){
$target_tables = [ 'prefix_table' ];
$target_shortcodes = [ 'cdbt-view', 'cdbt-edit' ];
$userid_column_name = 'user_id';
if ( in_array( $shortcode_name, $target_shortcodes ) && in_array( $table, $target_tables ) && 'and' === $narrow_operator ) {
$_current_user_id = 0; // For guest user
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$_current_user_id = $current_user->ID;
}
if ( empty( $conditions ) ) {
$conditions[$userid_column_name] = $_current_user_id;
} else {
$conditions = array_merge( $conditions, [ $userid_column_name => $_current_user_id ] );
}
}
return $conditions;
}
add_filter( 'cdbt_shortcode_query_conditions', 'my_filter_cdbt_shortcode_query_conditions', 10, 4 );
Reference
Related Methods
- get_data()指定のテーブルから任意のデータを取得します
- find_data()指定されたキーワードを含むデータを検索します
Related Hooks
- cdbt_crud_get_data_sqlget_data メソッドのSQLクエリをフィルターします
- cdbt_crud_find_data_sqlfind_data メソッドで発行されたSQLクエリをフィルターします