もともと自作プラグインにて「固定ページ」+「カスタムフィールド」で表示前に処理を行っていました。
pre_get_postsフックで表示前に処理を実行
add_action('pre_get_posts', array($this, 'my_pre_get_posts' ));
php
1public function my_pre_get_posts($query){ 2 // 管理画面 or メインクエリーではない or 固定ページのオブジェクトが無い 場合は処理中止 3 if (is_admin() || ! $query->is_main_query() || empty(get_queried_object()) ) { 4 return; 5 } 6 // 以後、カスタムフィールドの値で処理 7}
このようなコードで問題なく処理が出来ていました。
その後「カスタム投稿タイプ」というのが使えることを知り、コードを調整しました。
自作プラグインのコンストラクタでカスタム投稿タイプを追加
register_post_type( 'custom-page', $args);
しかし、上記の get_queried_object()
でオブジェクトが取得できなくなってしまいました。
そのため以後の処理コードを実行することが出来ません。
$query
の中身を下記ページで比較してみました。
・固定ページ queried_object 有り
・カスタム投稿ページ queried_object 無し
・存在しないページ queried_object 無し
$queryの出力を比較してみたところ「存在しないページ」と同じようになっています。
しかし、最終的にはページはちゃんと表示されています。
実際のカスタム投稿の$query内容がこちらです。
php
1WP_Query Object 2( 3 [query] => Array 4 ( 5 [page] => 6 [custom-page] => parent/children 7 [post_type] => custom-page 8 [name] => parent/children 9 ) 10 11 [query_vars] => Array 12 ( 13 [page] => 14 [custom-page] => parent/children 15 [post_type] => custom-page 16 [name] => parent/children 17 [error] => 18 [m] => 19 [p] => 0 20 [post_parent] => 21 [subpost] => 22 [subpost_id] => 23 [attachment] => 24 [attachment_id] => 0 25 [static] => 26 [pagename] => 27 [page_id] => 0 28 [second] => 29 [minute] => 30 [hour] => 31 [day] => 0 32 [monthnum] => 0 33 [year] => 0 34 [w] => 0 35 [category_name] => 36 [tag] => 37 [cat] => 38 [tag_id] => 39 [author] => 40 [author_name] => 41 [feed] => 42 [tb] => 43 [paged] => 0 44 [meta_key] => 45 [meta_value] => 46 [preview] => 47 [s] => 48 [sentence] => 49 [title] => 50 [fields] => 51 [menu_order] => 52 [embed] => 53 [category__in] => Array 54 ( 55 ) 56 57 [category__not_in] => Array 58 ( 59 ) 60 61 [category__and] => Array 62 ( 63 ) 64 65 [post__in] => Array 66 ( 67 ) 68 69 [post__not_in] => Array 70 ( 71 ) 72 73 [post_name__in] => Array 74 ( 75 ) 76 77 [tag__in] => Array 78 ( 79 ) 80 81 [tag__not_in] => Array 82 ( 83 ) 84 85 [tag__and] => Array 86 ( 87 ) 88 89 [tag_slug__in] => Array 90 ( 91 ) 92 93 [tag_slug__and] => Array 94 ( 95 ) 96 97 [post_parent__in] => Array 98 ( 99 ) 100 101 [post_parent__not_in] => Array 102 ( 103 ) 104 105 [author__in] => Array 106 ( 107 ) 108 109 [author__not_in] => Array 110 ( 111 ) 112 113 ) 114 115 [tax_query] => 116 [meta_query] => 117 [date_query] => 118 [post_count] => 0 119 [current_post] => -1 120 [in_the_loop] => 121 [comment_count] => 0 122 [current_comment] => -1 123 [found_posts] => 0 124 [max_num_pages] => 0 125 [max_num_comment_pages] => 0 126 [is_single] => 1 127 [is_preview] => 128 [is_page] => 129 [is_archive] => 130 [is_date] => 131 [is_year] => 132 [is_month] => 133 [is_day] => 134 [is_time] => 135 [is_author] => 136 [is_category] => 137 [is_tag] => 138 [is_tax] => 139 [is_search] => 140 [is_feed] => 141 [is_comment_feed] => 142 [is_trackback] => 143 [is_home] => 144 [is_404] => 145 [is_embed] => 146 [is_paged] => 147 [is_admin] => 148 [is_attachment] => 149 [is_singular] => 1 150 [is_robots] => 151 [is_posts_page] => 152 [is_post_type_archive] => 153 [query_vars_hash:WP_Query:private] => f0da19eaf2b3e3204ad56b687e2b4ecc 154 [query_vars_changed:WP_Query:private] => 155 [thumbnails_cached] => 156 [stopwords:WP_Query:private] => 157 [compat_fields:WP_Query:private] => Array 158 ( 159 [0] => query_vars_hash 160 [1] => query_vars_changed 161 ) 162 163 [compat_methods:WP_Query:private] => Array 164 ( 165 [0] => init_query_flags 166 [1] => parse_tax_query 167 ) 168 169)
今のところ、ページを特定できそうな情報としては get_query_var('name')
だけのように思えます。
子ページを持っている場合は、「親ページ/子ページ」の値が格納されています。
get_queried_object()
のように表示しようとしているページのデータを取得するにはどのような方法があるでしょうか?
【追記】
get_page_by_path()
で name からページ情報が取得できるようだったので、コードを次のようにしてテストしました。
結果、無事にデータを取得することが出来ました。
php
1public function my_pre_get_posts($query){ 2 print_r( get_page_by_path(get_query_var('name'),OBJECT,get_query_var('post_type') ));

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。