admin管理员组

文章数量:1122832

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();
           ?>

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();
           ?>

Share Improve this question asked Aug 21, 2024 at 6:13 pagolpagol 2032 gold badges4 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You need to first store your sub category id and pass that to WP_Query. Below is the logic how we can store the child category value.

$categories = get_the_category();

// store the subcategory id
$sub_category_id = [];
foreach($categories as $category) {
    if( $category->parent > 0 ) {
        $sub_category_id[] = $category->term_id;
    }
}

$args = array(
    'post_type' => 'post',
    'category' => $sub_category_id,
    'tag' => 'city', 
    'posts_per_page' => -1
);
$query = new WP_Query($args);

yes finally i made it

 <?php
$current_category = get_the_category();
$category_id = $current_category[0]->term_id;

$args = array(
  'cat' => $category_id,
  'post_type' => 'post', // Adjust if you're using custom post types
  'tag' => 'city',
  'posts_per_page' => -1 // Display all posts
);

$category_posts = new WP_Query($args);

if ($category_posts->have_posts()) : ?>
  <h2>Posts in the <?php echo $current_category[0]->name; ?> category:</h2>
  <ul>
    <?php while ($category_posts->have_posts()) : $category_posts->the_post(); ?>
      <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
  </ul>
<?php else : ?>
  <p>No posts found in this category.</p>
<?php endif;

wp_reset_postdata();

?>

本文标签: categoriesHow to display posts from a specific tag in the current category on a single page