各ターム内の記事全体から、各入力項目に対する入力の有無をカウントしたいと考えています。
全体の想定では
0. WP_Queryでタームごとに記事を一覧表示
0. 各入力項目件数を取得し変数に格納
0. ループ内で変数を条件分岐で比較
を想定しています。
①は以下の処理でタームごとに記事一覧を表示する事ができました。
<?php $target_post = 'post_type'; $target_post_cat = 'term_name'; $post_count = -1; $cat_args = array( 'parent' => 0, //トップレベルのタームのみ 'hierarchical' => 0 //子タームを含めない ); $cats = get_terms($target_post_cat, $cat_args); foreach( $cats as $cat ): ?> <?php $target_cat_slug = esc_html($cat -> slug); $target_cat_name = esc_html($cat -> name); ?> <?php $args = array( 'post_type' => array($target_post), 'taxonomy' => $target_post_cat, 'term' => $target_cat_slug, 'post_status' => 'publish', 'posts_per_page' => $post_count, // 表示するページ数 'orderby' => 'menu_order', 'order' => 'DESC' ,// 並び順 ); $my_query = new WP_Query($args); ?>
② 各入力項目件数を取得し変数に格納
について方法がわかりません。
各ターム内の記事全体をみて、
タームごとに
タイトルが入力されている件数 = $titleに入れたい
本文が入力されている件数 = $contentに入れたい
カスタムフィールド「A」(テキスト)が入力されている件数 =$aに入れたい
以下のサイトを参考にいろいろチャレンジしてみましたが、まったく兆しが見えません。
・WordPressで特定のカスタムフィールドの値を持つ投稿の数を取得する
https://hacknote.jp/archives/11762/
・WordPress WP_Query 複数のカスタムフィールドを条件に投稿を取得
https://chaika.hatenablog.com/entry/2018/03/12/090000
・もう迷わんよ。WordPressで固定ページや投稿ページが持つ様々な情報を取得する方法まとめ[基本情報・投稿タイプ・カスタムフィールド・カテゴリ・タグ・タクソノミーなど]
https://wemo.tech/400
③ ループ内で変数を条件分岐で比較
まだ、②ができないのですが想定では以下のように考えています。
<?php if( $my_query->have_posts() ) : ?> <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?> <?php if($title > 0 ) : ?> ~特定の処理~ <?php endif; ?> <?php if($content > 0 ) : ?> ~特定の処理~ <?php endif; ?> <?php endwhile; ?> <?php endif; ?>
ご教示のほど、よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。