I am trying to get all posts with a specific tag from the current category on a single page. However, my code is showing posts from the main category instead of the current subcategory. Let me explain: I have a main category called 'Country' and subcategories like 'Japan', 'China', etc. In the 'Japan' category, I have several posts, and some of them are tagged with 'City'. On a single page, I want to display a list of posts tagged with 'City' from the 'Japan' category, including the values of ACF (Advanced Custom Fields).
<?php
// Get the current category ID
$current_category_id = get_queried_object_id();
// Retrieve posts tagged with the current category "city"
$args = array(
'post_type' => 'post', // Adjust to your post type
'category' => $current_category_id,
'tag' => 'city', // Ensure posts are tagged with "city"
'posts_per_page' => -1 // Display all posts
);
$query = new WP_Query($args);
if ($query->have_posts()) : ?>
<ul>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
// Display ACF values here (replace 'your_acf_field_name' with the actual field name)
$acf_value = get_field('your_acf_field_name');
if ($acf_value) :
echo '<p>' . $acf_value . '</p>';
endif;
?>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
wp_reset_postdata();
?>