You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress指定分类下自定义文章类型查询及管理员置顶实现问题

解决方案

你可以通过分两次查询的方式实现需求,同时优化原代码的查询效率,修改后的完整代码如下:

<?php
/**
 * 自定义分类法position筛选作者自定义文章类型模板
 * Template Name: taxonomies
 *
 */

defined( 'ABSPATH' ) || exit; // 禁止直接访问

get_header(); ?>

<?php
// 获取当前页面标题,对应position分类项名称
$page_names = get_the_title();
// 提前获取对应position分类项,避免循环内判断
$position_term = get_term_by('name', $page_names, 'position');
?>

<section id="articles" class="articles">
    <div class="<?php echo $classcase ?>">
    <?php if ($position_term) : ?>
        <?php
        // 第一步:查询排在第一位的manager
        $manager_query = new WP_Query([
            'post_type' => 'authors',
            'posts_per_page' => 1, // 如果有多个manager可改为-1
            'tax_query' => [
                'relation' => 'AND',
                // 筛选当前页面对应的position分类
                [
                    'taxonomy' => 'position',
                    'field' => 'term_id',
                    'terms' => $position_term->term_id,
                ],
                // 筛选manager分类
                [
                    'taxonomy' => 'position',
                    'field' => 'slug',
                    'terms' => 'manager',
                ]
            ]
        ]);
        if ($manager_query->have_posts()) :
            while($manager_query->have_posts()): $manager_query->the_post();
        ?>
            <div class="tax-container">
                <a href="<?php the_permalink(); ?>">
                    <div class="parent-before">
                        <img class="tax-img" src="<?php the_post_thumbnail_url(); ?>" data-tool-tip="<?php the_title(); ?>" />
                        <div class="tax-div-p"  data-tool-tip="<?php the_title(); ?>">
                            <p class="tax-p"  data-tool-tip="<?php the_title(); ?>"></p>
                        </div>
                    </div>
                </a>
            </div>
        <?php 
            endwhile;
            wp_reset_postdata();
        endif;

        // 第二步:查询剩余team成员,按标题升序排序
        $team_query = new WP_Query([
            'post_type' => 'authors',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order' => 'ASC',
            'tax_query' => [
                'relation' => 'AND',
                [
                    'taxonomy' => 'position',
                    'field' => 'term_id',
                    'terms' => $position_term->term_id,
                ],
                // 筛选team分类
                [
                    'taxonomy' => 'position',
                    'field' => 'slug',
                    'terms' => 'team',
                ]
            ]
        ]);
        if ($team_query->have_posts()) :
            while($team_query->have_posts()): $team_query->the_post();
        ?>
            <div class="tax-container">
                <a href="<?php the_permalink(); ?>">
                    <div class="parent-before">
                        <img class="tax-img" src="<?php the_post_thumbnail_url(); ?>" data-tool-tip="<?php the_title(); ?>" />
                        <div class="tax-div-p"  data-tool-tip="<?php the_title(); ?>">
                            <p class="tax-p"  data-tool-tip="<?php the_title(); ?>"></p>
                        </div>
                    </div>
                </a>
            </div>
        <?php 
            endwhile;
            wp_reset_postdata();
        endif;
        ?>
    <?php endif; ?>
    </div>
</section>

<div class='endauthors'></div>

<?php get_footer(); ?>

主要修改说明

  • 新增分类预查询逻辑,直接在WP_Query中加入分类过滤参数,替代原代码全量查询所有作者后逐一判断的逻辑,大幅提升查询效率
  • 分两次查询分别获取manager和team成员,天然满足manager排在第一位的需求,不需要额外自定义排序规则
  • 修复了原代码中作者关联多个position分类时可能重复输出的问题
  • 注意代码中用到的$classcase变量请确保提前赋值,避免报错

内容的提问来源于stack exchange,提问作者eng malek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 08:36:03