wordpress - 过滤 WordPress 中的查询
问题描述
为了显示帖子类型中的一些项目,我使用了这个 WordPress 查询:
$posts = get_posts(array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC'
));
但是如何根据帖子类型页面中的信息过滤此查询返回的数据?例如,我有一个输入“年份”来获取项目的年份。
谢谢。
解决方案
您可以像下面一样使用 wp_query
$args = array (
'post_type' => array( 'realisations' ),
'post_status' => array( 'publish' ),
'order' => 'ASC',
'orderby' => 'date',
'year' => 'yourinputyear' // 2021
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post();
echo get_the_title();
endwhile;
}else{
echo "Data not found";
}
请尝试这种方式。希望有用。谢谢
推荐阅读
- kubernetes - 无法在 kubeless 上部署功能
- mysql - RMySQL 或 RMariaDB 连接到 MySQL 8 DB?
- javascript - 如何在javascript中将字符串转换为化学式
- javascript - 为什么在循环调用 readFile 时几乎同时调用回调?
- java - 我们可以通过在 Java 中使用可变长度参数来实现动态数组吗?
- salesforce - 如何在 Salesforce 中读取图像 URL 并使用 Salesforce 显示图像
- php - 如何将具有相同日期但不同时间的 json 数组转换为另一种格式的 json 数组,该数组将时间和 id 属性分组为相同的日期?
- python - 对多维数组中的值(前 5 个)进行排序
- oracle - Pl/SQL 函数返回具有更改字符的字符串
- reactjs - Invariant Violation:“无效的钩子调用。钩子只能放置在函数组件的主体内部。”