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

WordPress精选帖子查询优化:无需取消旧精选,仅显示最新精选

解决WordPress自动显示最新精选Podcast的问题

你的需求是让查询自动返回最新发布的带destacado标签的Podcast,无需手动取消旧帖子的精选标签。只需要给get_posts添加排序参数就能实现:

修改后的完整代码

<?php
global $post;

$myposts = get_posts([
    'posts_per_page' => 1,
    'post_type'      => 'podcast',
    'tax_query'      => [
        [
            'taxonomy' => 'post_tag',
            'field'    => 'slug',
            'terms'    => 'destacado',
        ]
    ],
    // 核心改动:按发布时间倒序排列,最新帖子优先
    'orderby'        => 'date',
    'order'          => 'DESC',
]);

if ($myposts) {
    foreach ($myposts as $post) :
        setup_postdata($post);
        // 这里写你的帖子内容输出逻辑
?>

关键说明

  • 添加orderby => 'date'和order => 'DESC'后,查询会自动按帖子发布时间从新到旧排序,取第一个结果就是最新的精选Podcast。
  • 同时简化了原代码中冗余的array_terms数组,直接把标签查询条件写入tax_query,代码更简洁易读。

以后新增精选Podcast时,不用手动移除旧帖子的destacado标签,前端会自动显示最新的那一篇。

内容的提问来源于stack exchange,提问作者Jake the dog

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 02:27:21