简体   繁体   English

wordpress通过帖子类型的自定义字段查询帖子

[英]wordpress query post by a custom field that is post type

I am using Advanced Custom Fields. 我正在使用高级自定义字段。

I have this query that gets all the post that have a custom field value for "artist" equal to the current page ID. 我有这个查询,使所有具有“艺术家”的自定义字段值等于当前页面ID的帖子。

query_posts('meta_key=artist&meta_value='.$postID.'&post_type=page&order_by=title&order=DESC');

This works great as long as the custom field only has a number in it. 只要自定义字段中只有一个数字,它就可以很好地工作。 I want to make my custom field a "post" . 我想将自定义字段设置为“ post”。 This isn't a problem because ACF has that option. 这不是问题,因为ACF具有该选项。 But since the meta_value in my query will now need to get a value from an array I'm not sure how to do it. 但是由于查询中的meta_value现在需要从数组中获取值,所以我不确定如何执行此操作。

Since the Post Object field is used to generate the custom field, you don't need meta key/value for the query, you just need to check if the field exist. 由于“ 发布对象”字段用于生成自定义字段,因此您不需要查询的元键/值,只需检查该字段是否存在。

So the query wold look something like this: 因此查询将如下所示:

$all_pages = get_posts('post_type=page&order_by=title&order=DESC');
foreach($all_pages as $page) {
    if(get_field('artist', $page->ID)) { // here you check if field is populated
       // loop content
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM