我已经尝试了几个小时来完成这项工作 - 但由于某种原因,这对我来说太困难了。我有一个自定义 post_type 'house',我想使用 meta_key 和某些元值找到我的自定义 post_type 的 post_id。
假设我想找到带有meta_key='house_id'meta_value='231sd1223'的房子的post_id
我到底该如何使用 wp->query 来做到这一点?
这里即使有循环也有查询。然而,查询元值会产生更多的数据库查询,请考虑循环遍历“house”帖子类型,而不是仅在 meta_value 等于门牌号时才执行某些操作。
// WP_Query arguments
$args = array (
'post_type' => array( 'house' ),
'post_status' => array( 'publish' ),
'meta_query' => array(
array(
'key' => 'house_id',
'value' => '231sd1223',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14207 次 |
| 最近记录: |