php - WordPress,查询未找到相关帖子
问题描述
我有三个帖子,两个有标签eGuide
,一个有标签Article
。当我单击 时eGuide
,我希望侧边栏显示其他 eGuide(最多 2 个)而不是其他任何内容。为此,我有以下查询:
global $post;
$args = array(
'post_type' => 'resources',
'category__in' => wp_get_post_categories($post->ID ),
'posts_per_page' => 3,
'post__not_in' => array($post->ID )
);
$relatedPosts = new WP_Query( $args );
但它也显示了article
?我也试过:
'post__not_in' => array(get_the_ID() )
......仍然没有运气。
解决方案
global $post;
$term_list = wp_get_post_terms( $post->ID, 'your_taxonomy_here', array( "fields" => "ids", "parent" => 0 ) );
$args = array (
'post_type' => 'resources',
'posts_per_page' => 3,
'post__not_in' => array( $post->ID ),
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'your_taxonomy_here',
'field' => 'term_id',
'terms' => $term_list[0],
),
),
);
$relatedPosts = new WP_Query( $args );
您可以尝试此代码以获取相关帖子。
推荐阅读
- ios - 排错错误:UICollectionViewFlowLayout 的行为未定义,因为项目高度必须小于高度
- sql - 如果满足条件,则限制内连接
- javascript - 检查复选框在 React 中不起作用
- c# - 为什么我们需要从 ASP.NET Core 2.1 迁移到 3.1
- c - 为什么 argv 在我的 execve 系统调用监视器中总是(NULL)
- cron - 如何在 hazelcast IScheduledExecutorService 中执行 cron 分布式任务?
- git - macOS 上的 git clone 仅适用于某些 Gitlab 实例
- python - Python selenium 无法单击按钮执行脚本
- kubernetes - Kubernetes 服务如何将流量路由到 https
- python - 美丽的汤对象在它附加到更改的请求对象后仍然有效