いつもお世話になっております。
taxonomy-products_cat.php
single-products.php
で、いわゆる関連投稿として、同じタームの投稿を取得しています。
取得した投稿の並び方を、ACFで作成したフィールドの値で数字の小さい順にしたいため、
下記のようにしたのですが、投稿記事は取得できるものの順番は無視されます。
'orderby' => 'meta_value_num', で数値として扱い、
'meta_key' => 'products_price', でキーを指定しているのですが無反応です。
なお、products_price の値は、「200,000」などカンマ区切りの価格になります。
php
<?php $taxonomy_slug = 'products_cat'; $post_type_slug = 'products'; $post_terms = wp_get_object_terms($post->ID, $taxonomy_slug); if( $post_terms && !is_wp_error($post_terms)) { $terms_slug = array(); foreach( $post_terms as $value ){ $terms_slug[] = $value->slug; } } $args = array( 'post_type' => $post_type_slug, 'posts_per_page' => -1, 'orderby' => 'meta_value_num', 'meta_key' => 'products_price', //ACFのフィールド名 'order' => 'ASC', 'post__not_in' => array($post->ID), 'tax_query' => array( array( 'taxonomy' => $taxonomy_slug, 'field' => 'slug', 'terms' => $terms_slug ) ) ); $the_query = new WP_Query($args); if($the_query->have_posts()): ?> <?php while ($the_query->have_posts()): $the_query->the_post(); ?> <div> <a href="<?php the_permalink(); ?>"> <?php $post_title = get_the_title(); the_post_thumbnail('full', array( 'alt' => $post_title, 'title' => $post_title)); ?> <p class="price"><?php the_field('products_price'); ?>円</p> </a> </div> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php endif; ?>
どなたかお分かりになる方、ご教授いただければと存じます。
恐れ入りますが、よろしくお願い申し上げます。
まだ回答がついていません
会員登録して回答してみよう