https://developer.wordpress.org/reference/hooks/the_title/
add_filter( 'the_title', 'wpb_sponsored' ); function wpb_sponsored( $title ) { global $post; $sponsored_text = '<span class="sponsored_text"> Sponsored Post</span> '; $sponsored = get_post_meta($post->ID, 'sponsored', true); if( $sponsored == 'true' && in_the_loop() ){ return $sponsored_text.$title; } return $title; }https://www.wpbeginner.com/wp-tutorials/how-to-add-sponsored-post-prefix-to-post-title-in-wordpress/ 2023
↓引数2つでpost_idをとるのがよい
add_filter( 'the_title', 'wpb_sponsored', 10, 2 ); function wpb_sponsored( $title, $post_id ) { $sponsored_text = '<span class="sponsored_text"> Sponsored Post</span> '; $sponsored = get_post_meta($post_id, 'sponsored', true); if( $sponsored == 'true' ){ return $sponsored_text.$title; } return $title; }
サブループ(Secondary Loops)
WordPress が自動的に取得したデータ(メインクエリ/$wp_query)とは異なる条件のデータ(サブクエリ)を使って表示するループをサブループや Secondary Loops と呼びます。
https://developer.wordpress.org/reference/functions/in_the_loop/
in_the_loop() はメインクエリglobal $wp_query; を参照するので
サブループ(Secondary Loops)のループの中かの判定には使えない。
いっぽう、query_posts関数を使用せずに、get_posts関数を使用してループをカスタマイズする方法があります。
この場合は、$wp_queryオブジェクトは書き換えられず、あくまでも「メインループ」とは別のループとして動作します。
https://morilog.com/wordpress/tips/is_main_query_in_the_loop/
WordPressの表示フローとLoopとthe_title()〜いろいろ考えたメモ〜 | Webourgeon 2013
ループ
投稿タイプ
post page attachment revision nav_menu_item custom_css customize_changeset
nav_menu_itemもpostとして保存されている。
WordPress の投稿タイプ 2022
add_filter
第1引数:フックする箇所
第2引数:追加挿入する処理を書いた関数
第3引数:優先順位(省略可能で初期値は10、値が小さいほど優先)
第4引数:関数が受け取る引数の個数(省略可能で初期値は1)WordPressプラグイン処理の実行後にフックして処理を追加挿入する方法 – add_filter()の優先順位の変更 | 日常スコープ 2018