如何在WP_Query查询中返回指定Hardware文章的所属分类?
Hey there! Let's work through this together. First, I spotted a tiny syntax mistake in your original code: new array("71", "10"); isn't valid PHP syntax—you just need to initialize the array directly.
Now, to pull in the associated categories (or custom taxonomies) for your 'Hardware' post type, here are two practical approaches:
方法1:在循环内直接获取分类(最常用)
WP_Query doesn't automatically include taxonomy data in the post objects by default, so the simplest way is to fetch the terms for each post as you loop through the results. Just use the get_the_terms() function, and make sure to use the correct taxonomy slug (if you're using a custom taxonomy for 'Hardware' instead of the default 'category').
Here's your updated, working code:
// Fix the array initialization first $posts = array(71, 10); // Post IDs are integers, so strings aren't necessary $query = new WP_Query(array( 'posts_per_page' => -1, 'post_type' => 'Hardware', 'post__in' => $posts, 'orderby' => 'post__in', // Optional: keeps posts in the order of your $posts array )); // Loop through the results if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); // Get the terms for the post - replace 'category' with your custom taxonomy slug if needed $categories = get_the_terms(get_the_ID(), 'category'); if ($categories && !is_wp_error($categories)) { echo '<h3>' . get_the_title() . '</h3>'; echo '<p>所属分类:'; foreach ($categories as $category) { // Output category name (or add a link using get_category_link()) echo '<span>' . esc_html($category->name) . '</span> '; } echo '</p>'; } } // Reset post data to avoid conflicts wp_reset_postdata(); }
注意事项:
- If your 'Hardware' post type uses a custom taxonomy (like 'hardware_category'), replace
'category'inget_the_terms()with your taxonomy's slug. - We added
'orderby' => 'post__in'to keep the posts in the same order as your original $posts array—this is optional but often useful.
方法2:批量预加载分类数据(性能优化)
If you're working with a lot of posts, fetching terms one by one can add extra database queries. Instead, you can use wp_get_object_terms() to pull all terms for your post IDs in a single query:
$posts = array(71, 10); $query = new WP_Query(array( 'posts_per_page' => -1, 'post_type' => 'Hardware', 'post__in' => $posts, 'orderby' => 'post__in', )); // Preload all terms for the post IDs in one go $all_categories = wp_get_object_terms($posts, 'category', array('fields' => 'all_with_object_id')); // Organize terms by post ID for easy access $categories_by_post = array(); foreach ($all_categories as $category) { $categories_by_post[$category->object_id][] = $category; } // Loop through posts if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); echo '<h3>' . get_the_title() . '</h3>'; if (isset($categories_by_post[$post_id])) { echo '<p>所属分类:'; foreach ($categories_by_post[$post_id] as $category) { echo '<span>' . esc_html($category->name) . '</span> '; } echo '</p>'; } } wp_reset_postdata(); }
This cuts down on database calls, making the code more efficient for larger datasets.
内容的提问来源于stack exchange,提问作者Carol.Kar

