Ecommerce Shopify WordPress Discussion

WP_Query not woking on a page template

I have a page template with an ACF field that displays specific categories on a page. The code is displaying all posts from all categories, but I want to display only the categories selected in the custom field. Hope you can help me. Thanks <?php /* Template Name: Category Page 2*/ ?> <?php get_header(); ?> <div class="container"> <div class="row"> <div class="col-md-12"> <?php $terms = get_field('select_cat'); if ($terms) : $page_cat = array(); ?> <div class="mt-3"> <span class="filter_title">Filter :</span> <select class="filter_dropdown"> <option value="">All</option> <?php foreach ($terms as $term) : ?> <option value="<?php echo esc_url(get_term_link($term)); ?>" data-thumbnail="<?php echo get_field('flag_image' , esc_html($term->taxonomy . '_' . $term->term_id)); ?>"><?php echo esc_html($term->name); ?></option> <?php endforeach; ?> </select> </div> <?php endif; ?> </div> </div> </div> <?php //print_r($page_cat); ?> <?php $posts = new WP_Query([ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 9, 'cat' => $page_cat, 'orderby' => 'date', 'order' => 'DESC', 'paged' => 1, ]); ?> <div class="container"> <div class="row"> <div class="col-md-8"> <?php if ($posts->have_posts()) : ?> <div class="row post-list"> <?php $i = 1; while ($posts->have_posts()) : $posts->the_post(); ?> <?php $author_id = get_the_author_meta('ID'); $profilepicture = get_user_meta( $author_id, 'profilepicture', 'true' ); $display_name = get_the_author_meta( 'display_name', $author_id ); ?> <div class="catpage_post_col col-md-<?php if ($i == 1) {echo "12";}else{echo "6";} ?>"> <div> <a href="<?php echo get_permalink(); ?>"><img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>" class="img-fluid catpage_post_feature_img<?php if ($i == 1) {echo '-01';}?>"></a> </div> <div> <span class="post_author_info"> <img src="<?php echo $profilepicture; ?>" class="post_author_img"> <span class="single_post_author_name"> <a href="<?php echo esc_url( get_author_posts_url( $author_id ) ); ?>"><?php the_author(); ?></a> </span> </span> <span class="post_date_info"> <?php echo get_the_date(); ?> </span> <?php if ($i == 1) { ?> <span class="post_read_info"> <?php echo do_shortcode('[rt_reading_time postfix="MINS READ" postfix_singular="minute"]') ?> </span> <?php } ?> </div> <h4 class="catpage_post_title"><a href="<?php echo get_permalink(); ?>" class="catpage_post_title_link"><?php echo get_the_title(); ?></a></h4> <p class="catpage_post_excerpt"> <?php $excerpt = get_the_excerpt(); $excerpt = substr( $excerpt, 0, 150 ); $result = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) ); echo $result; ?> </p> <a href="<?php echo get_permalink(); ?>" class="catpage_post_link">CONTINUE READING</a> </div> <?php $i++; endwhile; ?> </div> <?php endif; ?> <?php wp_reset_postdata(); ?> <div class="vc_btn3-container with-icon without_col_change vc_btn3-center text-center mb-5 load-more-div"> <a class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-round vc_btn3-style-classic vc_btn3-color-success d-inline-block load-more" href="#">Load more</a> </div> </div> <div class="col-md-4 post-sidebar"> <?php dynamic_sidebar( 'sidebar01' ); ?> </div> </div> </div> <?php get_footer(); ?> <script type="text/javascript"> let currentPage = 1; $('.load-more').on('click', function(event) { event.preventDefault(); currentPage++; // Do currentPage + 1, because we want to load the next page $.ajax({ type: 'POST', url: '/wp-admin/admin-ajax.php', dataType: 'json', data: { action: 'weichie_load_more', paged: currentPage, page_cat: <?php echo json_encode($page_cat); ?>, }, success: function(res) { $('.post-list').append(res.html); if (currentPage >= res.max) { $('.load-more-div').hide(); } } }); }); </script> <script> $(document).ready(function() { $(".filter_dropdown").change(function(){ window.location = $('.filter_dropdown').val(); }); }) </script> The code is displaying all posts from all categories, but I want to display only the categories selected in the custom field.
Assuming, you are adding taxonomy multi selection in "select_cat" ACF field. Please collect term_ids first. $term_ids = wp_list_pluck(get_field('select_cat'), 'term_id'); Now, Please update your wp query as below, $posts = new WP_Query([ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 9, 'tax_query' => array( array( 'taxonomy' => 'category', // Make sure you are using correct taxonomy name as needed 'field' => 'term_id', 'terms' => $term_ids ) ), 'orderby' => 'date', 'order' => 'DESC', 'paged' => 1, ]); Possibly, you want to have ajax based dropdown filter, load more. You can use the same logic in ajax callback function with your dropdown selection and pagination counter.

January 7, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.