WordPress Lab
カスタムフィールドの「日」の値が早い順でさらに「時間」の値が早い順に並べて出力するWP Query
2018年3月1日 木曜日イベント開催スケジュールを開催日の早い順でかつ同じ日で時間の早いイベント順に並べて出力する必要があったので、下記のようなWP Queryを書いて可能にしました。
<?php $args = array( 'meta_query' => array( 'customfield_01' => array( 'key' => 'start_date', //「日」のフィールド名 ), 'customfield_02' => array( 'key' => 'start_time', //「時間」のフィールド名 ) ), 'orderby' => array( 'customfield_01' => 'ASC', 'customfield_02' => 'ASC' ) ); $the_query = new WP_Query( $args );// ループ if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); endwhile; endif; // 投稿データをリセット wp_reset_postdata(); ?>
(参考)
WP_Query get_posts 複数のカスタムフィールドの値で順序制御
Order by multiple meta keys in WP Query
ユーザーの権限は直接取得ができない
2017年6月14日 水曜日投稿者ユーザーを登録するときに同時にカテゴリーや固定ページを立てるような仕組みを作っていたのですが
どうしても上手くいかない。
$user_info = get_userdata($user_id); if ( $user_info->roles == "author" ){ ・・・ }
拙い英語力を頼りにネットで探しまくってるとユーザーの権限は直接取得ができないとのこと。
いったん取得してから、次のように書くとよいらしい。
$user_info = get_userdata($user_id); $user_roles = $user_info->roles; if (in_array("author", $user_roles)){ ・・・ }
ユーザーの権限が直接取得ができない、なんて何見たらすぐわかるやろ...
試みた者の経験しかないのやろか…
投稿者ユーザー登録時に自動でユーザー専用の固定ページをつくる
2017年5月8日 月曜日(条件)
固定ページのスラッグはinfo-(ユーザー名)で、user-info_template.phpというページテンプレートを使う。
ページタイトルは「名」。
functions.php
function create_user_page ( $user_id ) { $user_info = get_userdata($user_id); $user_name = $user_info->user_login; $new_page = array( 'post_type' => 'page', 'post_title' => $user_info->first_name, 'post_name' => 'info-' . $user_name, 'post_status' => 'publish', 'post_author' => $user_id, 'page_template' => 'user-info_template.php' ); wp_insert_post($new_page); } add_action('user_register', 'create_user_page' );
ユーザーにこのページだけを編集させる制限などはUser Roll Editorなどで設定、紐付けしてください。
過日の投稿(「投稿者ユーザー登録時にユーザー名をスラッグでカテゴリーをつくる」)のコードとまとめて使うと便利です。
複数のカテゴリーに属している記事の特定の親の子カテゴリー名を表示する
2017年4月17日 月曜日複数のカテゴリーに属している記事の特定の親の子カテゴリー名を表示する
※ ただし1つの親に対して1つの子であること
<?php $categorys = get_the_category( $post_id ); foreach($categorys as $category){ if($category->parent==特定の親カテゴリーID): echo $category->name; endif; }; ?>
投稿者ユーザー登録時にユーザー名をスラッグでカテゴリーをつくる
2017年4月7日 金曜日(条件)
ユーザー名をカテゴリースラッグにする。
カテゴリー名は、ユーザー登録時の「名」。
functions.php
function create_category( $user_id ) { $user_info = get_userdata($user_id); $user_name = $user_info->user_login; $my_cat = array( 'cat_name' => $user_info->first_name, 'category_nicename' => $user_name, 'category_parent' => '' ); wp_insert_category( $my_cat ); $cat_id = get_cat_ID( $user_id ); wp_set_post_categories( $post->ID, array( $cat_id ), true ); } add_action('user_register', 'create_category' );
ユーザー登録と同時にユーザー名のカテゴリーが生成されますが、ログインユーザーがそのカテゴリーでしか投稿できないというような仕組みはAuthor Category等のプラグインで関連付けしてください。