ル-プの中で、補足のコ-ドを使い、取得するpostを条件づけて表示しています。
カスタムフィ-ルド(hogehoge)で設定した値を使って、絞り込みをやっています。
(カスタムフィ-ルドhogehogeに値があれば表示)。
ふと、postのカスタムフィ-ルドで絞り込むのではなく、投稿者の情報で絞り込めないものかと思い、質問させていただいています。
現在、投稿者(author)にカスタムフィ-ルドを設定しているのですが、そのフィ-ルドに値を保持している投稿者のpostだけを表示する、ということをしたいのです。
色々やってみるもののうまくいきません。
レアなケ-スなのか、ググってもよくわかりませんでした。
補足のコ-ドでは、postのカスタムフィ-ルド「hogehoge」で絞り込んでいるのですが、それの代わりに投稿者(author)のカスタムフィ-ルド(testes)の値あるなしで絞り込むためのコ-ドを教えてください(AND)。
よろしくお願いいたします。
$args = Array( 'post_type' => 'post', 'posts_per_page' => -1, 'meta_query' => array(array( 'key' => 'address', 'value' => $place, 'compare' => 'LIKE' ), array( 'key' => hogehoge", 'value' => 'null', 'compare' => '!=' ), ) );
ちょっと調べてみたのですけれど、WP_Query に渡す検索条件だけでは、希望している動作はできないと思います。
# SQL を書けばできるかもしれませんが
WP_Query では、今まで通りに住所とカテゴリーを絞り込んで、そのループの中で投稿者のカスタムフィールドを取得して、値が設定されているかどうかを確認するような感じでいけると思います。
<?php ... // address と、指定されていれば category で検索する $the_query = new WP_Query($args); if ($the_query->have_posts()): ?> <div id="map" style="width: 99%; height: 650px;"></div> <script type="text/javascript"> (function() { // ★ こんなので、くるんでみる var posts = []; <?php while ($the_query->have_posts()) : $the_query->the_post(); // 記事の投稿者を取得して、そのカスタムフィールドの値を取得する $author_id = $post->post_author; $author_testes = get_user_meta($author_id, "testes", true); // 投稿者のカスタムフィールド testes に値が設定されていたら... if ($author_testes != "") { $googlemap = get_field('lat'); if (! $googlemap) { // マップ住所が登録されていないこともある continue; } $category = get_the_category(); ?> posts.push({ lat: '<?php echo post_custom("lat")?>', lng: '<?php echo post_custom("lon")?>', name: '<?php echo post_custom("Name")?>', link: '<?php the_permalink();?>', title: '<?php the_title(); ?>', cat_id: '<?php echo $category[0]->category_nicename; ?>' }); <?php } // End of $author_hoge ?> <?php endwhile; // End the loop. Whew. ?>
ちょっと、ごちゃごちゃしているので、骨子になるところだけ分離すると、こんな感じ。
<?php ... // address と、指定されていれば category で検索する $the_query = new WP_Query($args); if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post(); // 記事の投稿者を取得して、そのカスタムフィールドの値を取得する $author_id = $post->post_author; $author_testes = get_user_meta($author_id, "testes", true); // 投稿者のカスタムフィールド testes に値が設定されていたら... if ($author_testes != "") { ?> ... // 記事のデータを javascript の配列に取り込み <?php } endwhile; else: ... endif; ?>
ちょっと調べてみたのですけれど、WP_Query に渡す検索条件だけでは、希望している動作はできないと思います。
# SQL を書けばできるかもしれませんが
WP_Query では、今まで通りに住所とカテゴリーを絞り込んで、そのループの中で投稿者のカスタムフィールドを取得して、値が設定されているかどうかを確認するような感じでいけると思います。
<?php ... // address と、指定されていれば category で検索する $the_query = new WP_Query($args); if ($the_query->have_posts()): ?> <div id="map" style="width: 99%; height: 650px;"></div> <script type="text/javascript"> (function() { // ★ こんなので、くるんでみる var posts = []; <?php while ($the_query->have_posts()) : $the_query->the_post(); // 記事の投稿者を取得して、そのカスタムフィールドの値を取得する $author_id = $post->post_author; $author_testes = get_user_meta($author_id, "testes", true); // 投稿者のカスタムフィールド testes に値が設定されていたら... if ($author_testes != "") { $googlemap = get_field('lat'); if (! $googlemap) { // マップ住所が登録されていないこともある continue; } $category = get_the_category(); ?> posts.push({ lat: '<?php echo post_custom("lat")?>', lng: '<?php echo post_custom("lon")?>', name: '<?php echo post_custom("Name")?>', link: '<?php the_permalink();?>', title: '<?php the_title(); ?>', cat_id: '<?php echo $category[0]->category_nicename; ?>' }); <?php } // End of $author_hoge ?> <?php endwhile; // End the loop. Whew. ?>
ちょっと、ごちゃごちゃしているので、骨子になるところだけ分離すると、こんな感じ。
<?php ... // address と、指定されていれば category で検索する $the_query = new WP_Query($args); if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post(); // 記事の投稿者を取得して、そのカスタムフィールドの値を取得する $author_id = $post->post_author; $author_testes = get_user_meta($author_id, "testes", true); // 投稿者のカスタムフィールド testes に値が設定されていたら... if ($author_testes != "") { ?> ... // 記事のデータを javascript の配列に取り込み <?php } endwhile; else: ... endif; ?>
なるほど、勉強になります。
ありがとうございます。
やはり一発でベストアンサ-をいただけましたね。
いつもお世話さまです!
なるほど、勉強になります。
2017/05/31 05:04:41ありがとうございます。
やはり一発でベストアンサ-をいただけましたね。
いつもお世話さまです!