admin管理员组文章数量:1122846
I have a CPT that contains Employee Bio information. Because we don't want to create accounts in wp_users for everyone, this data is implemented as a "regular" CPT living in wp_posts. Let's say I have a row with ID 250 for employee Scott.
We also have normal blog posts. Since we're not using regular WP users, we can't take advantage of the usual metadata and relationships to associate authors with posts. So instead, we have a custom field, "af_author", created with an ACF Relationship field type. When we create a post, we choose an author from the list of Employee Bio objects.
On the page that displays the bio information for an employee, we want to include a list of posts that the employee has authored. So when I'm looking at the Employee Bio page for Scott (ID = 250), I want to do a query loop to retrieve all Posts that have been written by ID = 250. Who the post is written by is stored in the related custom field "af_author" for each post. The field itself is an array of IDs; ID 250 would need to be "IN" the array.
I'm having trouble figuring out how to code the meta query for this. The code below is executed while creating the Employee Bio page, so if employee Scott is being displayed, get_the_ID()
would return 250. I have tried the following code with no success:
$new_query_args = [
'meta_query' => [
[
'key' => 'af_author',
'value' => get_the_ID(),
'compare' => 'IN',
]
]
];
$query_args = array_merge( $query_args, $new_query_args );
How do I make this work?
I have a CPT that contains Employee Bio information. Because we don't want to create accounts in wp_users for everyone, this data is implemented as a "regular" CPT living in wp_posts. Let's say I have a row with ID 250 for employee Scott.
We also have normal blog posts. Since we're not using regular WP users, we can't take advantage of the usual metadata and relationships to associate authors with posts. So instead, we have a custom field, "af_author", created with an ACF Relationship field type. When we create a post, we choose an author from the list of Employee Bio objects.
On the page that displays the bio information for an employee, we want to include a list of posts that the employee has authored. So when I'm looking at the Employee Bio page for Scott (ID = 250), I want to do a query loop to retrieve all Posts that have been written by ID = 250. Who the post is written by is stored in the related custom field "af_author" for each post. The field itself is an array of IDs; ID 250 would need to be "IN" the array.
I'm having trouble figuring out how to code the meta query for this. The code below is executed while creating the Employee Bio page, so if employee Scott is being displayed, get_the_ID()
would return 250. I have tried the following code with no success:
$new_query_args = [
'meta_query' => [
[
'key' => 'af_author',
'value' => get_the_ID(),
'compare' => 'IN',
]
]
];
$query_args = array_merge( $query_args, $new_query_args );
How do I make this work?
Share Improve this question edited Aug 2, 2024 at 18:01 scott8035 asked Aug 2, 2024 at 17:40 scott8035scott8035 2302 silver badges10 bronze badges 3 |1 Answer
Reset to default 0Based on https://stackoverflow.com/questions/42011047/wordpress-query-by-acf-relationship-field, I was able to fix this by changing the 'compare' => 'IN'
to 'compare' => 'LIKE'
. So the resultant code looks like this:
$new_query_args = [
'meta_query' => [
[
'key' => 'af_author',
'value' => get_the_ID(),
'compare' => 'LIKE',
]
]
];
$query_args = array_merge( $query_args, $new_query_args );
I'm not exactly sure why this works, but it fixed the problem.
本文标签: custom post typesMeta query with ACF relationship field
版权声明:本文标题:custom post types - Meta query with ACF relationship field 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299043a1930360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_the_ID
is going to return or the context. Also keep in mind thatmeta_query
is expensive and doesn't scale well, it would be much faster ( possibly orders of magnitude faster ) to use a taxonomy rather than post meta ( aka ACF field ). Also while ACF is offtopic here,meta_query
is on topic – Tom J Nowell ♦ Commented Aug 2, 2024 at 17:45query_args
created and what does it contain? Is this inside or outside the loop? Is your author page a custom template that uses aWP_Query
to fetch the author or is it a main loop in a standard template? When you say it's 250 is that what it should be, or have you checked and seen the value directly to confirm it? – Tom J Nowell ♦ Commented Aug 3, 2024 at 9:03